com.github.javaclub.ossclient.web.AssetsPreviewController Maven / Gradle / Ivy
package com.github.javaclub.ossclient.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.Channels;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.github.javaclub.ossclient.util.AssetsUtils;
import com.github.javaclub.toolbox.ToolBox.Base64;
import com.github.javaclub.toolbox.ToolBox.IO;
import com.github.javaclub.toolbox.ToolBox.Strings;
import com.github.javaclub.toolbox.ToolBox.Web;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
/**
* 服务器本机资源查看预览
* 文本/图片资源直接在线显示(txt/ini/html/json/conf/java/xml/properties/jpg/jpeg/png)
* 其他资源直接走下载方式
*/
@Controller
@Api(tags = "文件预览")
public class AssetsPreviewController {
@ApiOperation(value = "服务本机文件预览查看")
@ApiImplicitParam(name = "file", value = "服务器本机资源相对Path路径(路径分割符/换成!)", paramType = "path", dataType = "string", required = true)
@GetMapping("/rs/{file}")
public void previewFile(@PathVariable("file") String file,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
File localFile = AssetsUtils.localFile(file);
if (!localFile.exists()) {
// 返回404
Web.response(response, AssetsUtils.html404(), false, Strings.EMPTY, "UTF-8");
return;
}
String filepathAndName = Strings.replace(file, "!", "/");
if (AssetsUtils.canWebView(filepathAndName)) {
if (AssetsUtils.isImage(filepathAndName)) { // 图片预览
String ext = AssetsUtils.getFileExt(filepathAndName);
try (FileInputStream fis = new FileInputStream(localFile)) {
response.setContentType("image/" + ext);
fis.getChannel().transferTo(0, fis.available(), Channels.newChannel(response.getOutputStream()));
return;
} catch (Exception e) {
//
}
} else if (AssetsUtils.isPDF(filepathAndName)) {
byte[] bytes = IO.copyToByteArray(new FileInputStream(localFile));
String pdfBase64 = Base64.encodeToString(bytes);
pdfBase64.replaceAll("\r\n", "");
pdfBase64.replaceAll("\\+", "%2B");
response.setContentType("text/html; charset=UTF-8");
StringBuffer sb = new StringBuffer(
"\n" + "\n" + "\n"
+ "\n" + " \n"
+ " \n"
+ " \n"
+ " Document \n" + "\n" + "\n" + "\n"
+ "\n" + " \n" + "\n" + "\n"
+ "");
response.getWriter().print(sb.toString());
return;
} else { // 其他文本
try (FileInputStream fis = new FileInputStream(localFile)) {
response.setContentType("text/plain; charset=UTF-8");
fis.getChannel().transferTo(0, fis.available(), Channels.newChannel(response.getOutputStream()));
return;
} catch (Exception e) {
}
}
}
// 直接下载本地文件
int idx = filepathAndName.lastIndexOf("/");
String fileName = filepathAndName.substring(idx + 1);
// 读到流中
InputStream input = new FileInputStream(localFile);// 文件的存放路径
OutputStream output = response.getOutputStream();
// 设置输出的格式
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[512];
int len = 0;
try {
while ((len = input.read(b)) > 0) {
output.write(b, 0, len);
}
output.flush();
} catch (IOException e) {
throw e;
} finally {
IO.closeQuietly(output);
IO.closeQuietly(input);
}
}
}