|
@@ -68,6 +68,61 @@ public class DocFileServiceImpl implements IDocFileService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 预览文件(内联显示,不触发下载)
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void preview(Long id, HttpServletResponse response) {
|
|
|
|
|
+ DocFile docFile = docFileMapper.selectById(id);
|
|
|
|
|
+ if (docFile == null || docFile.getPath() == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ String name = docFile.getName();
|
|
|
|
|
+ String ext = name != null && name.contains(".") ? name.substring(name.lastIndexOf(".") + 1).toLowerCase() : "";
|
|
|
|
|
+ String contentType = guessContentType(ext);
|
|
|
|
|
+ response.setContentType(contentType);
|
|
|
|
|
+ // inline: 浏览器内联展示,不强制下载
|
|
|
|
|
+ response.setHeader("Content-Disposition", "inline; filename=" + encodeName(name));
|
|
|
|
|
+ response.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
|
+ FileUtils.writeBytes(docFile.getPath(), response.getOutputStream());
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String guessContentType(String ext) {
|
|
|
|
|
+ switch (ext) {
|
|
|
|
|
+ case "pdf": return "application/pdf";
|
|
|
|
|
+ case "png": return "image/png";
|
|
|
|
|
+ case "jpg": case "jpeg": return "image/jpeg";
|
|
|
|
|
+ case "gif": return "image/gif";
|
|
|
|
|
+ case "webp": return "image/webp";
|
|
|
|
|
+ case "bmp": return "image/bmp";
|
|
|
|
|
+ case "svg": return "image/svg+xml";
|
|
|
|
|
+ case "mp4": return "video/mp4";
|
|
|
|
|
+ case "webm": return "video/webm";
|
|
|
|
|
+ case "mp3": return "audio/mpeg";
|
|
|
|
|
+ case "wav": return "audio/wav";
|
|
|
|
|
+ case "txt": case "log": return "text/plain;charset=UTF-8";
|
|
|
|
|
+ case "html": case "htm": return "text/html;charset=UTF-8";
|
|
|
|
|
+ case "md": return "text/markdown;charset=UTF-8";
|
|
|
|
|
+ case "json": return "application/json;charset=UTF-8";
|
|
|
|
|
+ case "csv": return "text/csv;charset=UTF-8";
|
|
|
|
|
+ case "xml": return "text/xml;charset=UTF-8";
|
|
|
|
|
+ default: return "application/octet-stream";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String encodeName(String name) {
|
|
|
|
|
+ if (name == null) return "file";
|
|
|
|
|
+ try {
|
|
|
|
|
+ return java.net.URLEncoder.encode(name, "UTF-8").replace("+", "%20");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return "file";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 上传文件
|
|
* 上传文件
|
|
|
*/
|
|
*/
|