From 5d18e447fdf327bff36d4439f4820e4b3b34e452 Mon Sep 17 00:00:00 2001 From: woshiduanmu <1210893441@qq.com> Date: Wed, 13 Dec 2017 18:45:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0properties=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E9=A2=84=E8=A7=88=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yudianbank/utils/DownloadUtils.java | 4 ++ .../com/yudianbank/utils/OfficeToPdf.java | 18 -------- .../com/yudianbank/utils/ShedulerClean.java | 2 +- .../com/yudianbank/utils/SimTextUtil.java | 24 +++++++++++ .../controller/OnlinePreviewController.java | 43 +++++++++++-------- src/main/resources/application.properties | 4 +- src/main/resources/web/compress.ftl | 19 ++++++-- src/main/resources/web/picture.ftl | 4 +- 8 files changed, 74 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/yudianbank/utils/SimTextUtil.java diff --git a/src/main/java/com/yudianbank/utils/DownloadUtils.java b/src/main/java/com/yudianbank/utils/DownloadUtils.java index 5980feec..1be2f8f0 100644 --- a/src/main/java/com/yudianbank/utils/DownloadUtils.java +++ b/src/main/java/com/yudianbank/utils/DownloadUtils.java @@ -48,6 +48,8 @@ public class DownloadUtils { UUID uuid = UUID.randomUUID(); if (null == fileName) { fileName = uuid+ "."+type; + }else { // 文件后缀不一致时,以type为准(针对simText【将类txt文件转为txt】) + fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type); } String realPath = fileDir + fileName; File dirFile = new File(fileDir); @@ -67,6 +69,8 @@ public class DownloadUtils { os.close(); in.close(); response.setContent(realPath); + // 同样针对类txt文件,如果成功msg包含的是转换后的文件名 + response.setMsg(fileName); return response; } catch (IOException e) { e.printStackTrace(); diff --git a/src/main/java/com/yudianbank/utils/OfficeToPdf.java b/src/main/java/com/yudianbank/utils/OfficeToPdf.java index 884f7729..0a3efa63 100644 --- a/src/main/java/com/yudianbank/utils/OfficeToPdf.java +++ b/src/main/java/com/yudianbank/utils/OfficeToPdf.java @@ -22,24 +22,6 @@ public class OfficeToPdf { return office2pdf(inputFilePath, outputFilePath); } - - - /** - * 连接OpenOffice.org 并且启动OpenOffice.org - * - * @return - */ - /*public OfficeManager getOfficeManager() { - DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); - // 获取OpenOffice.org 3的安装目录 - String officeHome = openOfficePath; - config.setOfficeHome(officeHome); - // 启动OpenOffice的服务 - OfficeManager officeManager = config.buildOfficeManager(); - officeManager.start(); - return officeManager; - }*/ - /** * 转换文件 * diff --git a/src/main/java/com/yudianbank/utils/ShedulerClean.java b/src/main/java/com/yudianbank/utils/ShedulerClean.java index 761a464d..09ce9e1e 100644 --- a/src/main/java/com/yudianbank/utils/ShedulerClean.java +++ b/src/main/java/com/yudianbank/utils/ShedulerClean.java @@ -11,7 +11,7 @@ public class ShedulerClean { @Value("${file.dir}") String fileDir; - @Scheduled(cron = "0 0 23 * * ?") //每晚23点执行一次 +// @Scheduled(cron = "0 0 23 * * ?") //每晚23点执行一次 public void clean(){ System.out.println("执行一次清空文件夹"); DeleteFileUtil.deleteDirectory(fileDir); diff --git a/src/main/java/com/yudianbank/utils/SimTextUtil.java b/src/main/java/com/yudianbank/utils/SimTextUtil.java new file mode 100644 index 00000000..885be242 --- /dev/null +++ b/src/main/java/com/yudianbank/utils/SimTextUtil.java @@ -0,0 +1,24 @@ +package com.yudianbank.utils; + +import com.yudianbank.param.ReturnResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * 读取类文本文件 + * @author yudian-it + * @date 2017/12/13 + */ +@Component +public class SimTextUtil { + @Value("${file.dir}") + String fileDir; + @Autowired + DownloadUtils downloadUtils; + + public ReturnResponse readSimText(String url, String fileName, String needEncode){ + ReturnResponse response = downloadUtils.downLoad(url, "txt", fileName, needEncode); + return response; + } +} diff --git a/src/main/java/com/yudianbank/web/controller/OnlinePreviewController.java b/src/main/java/com/yudianbank/web/controller/OnlinePreviewController.java index eece64f0..6a69365b 100644 --- a/src/main/java/com/yudianbank/web/controller/OnlinePreviewController.java +++ b/src/main/java/com/yudianbank/web/controller/OnlinePreviewController.java @@ -1,12 +1,9 @@ package com.yudianbank.web.controller; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; import com.yudianbank.param.ReturnResponse; -import com.yudianbank.utils.DownloadUtils; -import com.yudianbank.utils.FileUtils; -import com.yudianbank.utils.OfficeToPdf; -import com.yudianbank.utils.ZipReader; +import com.yudianbank.utils.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; @@ -15,10 +12,10 @@ import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.util.Arrays; /** * @author yudian-it @@ -33,6 +30,10 @@ public class OnlinePreviewController { DownloadUtils downloadUtils; @Autowired ZipReader zipReader; + @Autowired + SimTextUtil simTextUtil; + @ApolloConfig + Config config; @Value("${file.dir}") String fileDir; @@ -49,31 +50,32 @@ public class OnlinePreviewController { @RequestMapping(value = "onlinePreview",method = RequestMethod.GET) public String onlinePreview(String url, String needEncode, Model model) throws UnsupportedEncodingException { // 路径转码 - url = URLDecoder.decode(url, "utf-8"); + String decodedUrl = URLDecoder.decode(url, "utf-8"); String type = typeFromUrl(url); String suffix = suffixFromUrl(url); + // 抽取文件并返回文件列表 + String fileName = fileUtils.getFileNameFromURL(decodedUrl); model.addAttribute("fileType", suffix); if (type.equalsIgnoreCase("picture")) { model.addAttribute("imgurl", url); return "picture"; - } else if (type.equalsIgnoreCase("txt") - || type.equalsIgnoreCase("html") - || type.equalsIgnoreCase("xml") - || type.equalsIgnoreCase("java") - || type.equalsIgnoreCase("properties") - || type.equalsIgnoreCase("mp3")){ - model.addAttribute("ordinaryUrl",url); + } else if (type.equalsIgnoreCase("simText")){ + ReturnResponse response = simTextUtil.readSimText(decodedUrl, fileName, needEncode); + if (0 != response.getCode()) { + model.addAttribute("msg", response.getMsg()); + return "fileNotSupported"; + } + model.addAttribute("ordinaryUrl", response.getMsg()); return "txt"; } else if(type.equalsIgnoreCase("pdf")){ model.addAttribute("pdfUrl",url); return "pdf"; } else if(type.equalsIgnoreCase("compress")){ // 抽取文件并返回文件列表 - String fileName = fileUtils.getFileNameFromURL(url); String fileTree = null; // 判断文件名是否存在(redis缓存读取) if (!StringUtils.hasText(fileUtils.getConvertedFile(fileName))) { - ReturnResponse response = downloadUtils.downLoad(url, suffix, fileName, needEncode); + ReturnResponse response = downloadUtils.downLoad(decodedUrl, suffix, fileName, needEncode); if (0 != response.getCode()) { model.addAttribute("msg", response.getMsg()); return "fileNotSupported"; @@ -99,7 +101,6 @@ public class OnlinePreviewController { return "fileNotSupported"; } } else if ("office".equalsIgnoreCase(type)) { - String fileName = fileUtils.getFileNameFromURL(url); String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + ((suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx")) ? "html" : "pdf"); @@ -107,7 +108,7 @@ public class OnlinePreviewController { if (!fileUtils.listConvertedFiles().containsKey(pdfName)) { String filePath = fileDir + fileName; if (!new File(filePath).exists()) { - ReturnResponse response = downloadUtils.downLoad(url, suffix, null, needEncode); + ReturnResponse response = downloadUtils.downLoad(decodedUrl, suffix, null, needEncode); if (0 != response.getCode()) { model.addAttribute("msg", response.getMsg()); return "fileNotSupported"; @@ -163,6 +164,10 @@ public class OnlinePreviewController { if (fileUtils.listOfficeTypes().contains(fileType.toLowerCase())) { fileType = "office"; } + if (Arrays.asList(config.getArrayProperty("simText",",",new String[]{})) + .contains(fileType.toLowerCase())) { + fileType = "simText"; + } return fileType; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 63fed3bb..abd2a467 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -20,4 +20,6 @@ office.home = C:\\Program Files (x86)\\OpenOffice 4 server.tomcat.uri-encoding = UTF-8 converted.file.charset = GBK #======================================#文件上传限制#======================================# -spring.http.multipart.max-file-size=100MB \ No newline at end of file +spring.http.multipart.max-file-size=100MB +## 支持的类文本格式的文件类型 +simText = txt,html,xml,java,properties,mp3,mp4,sql \ No newline at end of file diff --git a/src/main/resources/web/compress.ftl b/src/main/resources/web/compress.ftl index 96361a2b..0fd788a5 100644 --- a/src/main/resources/web/compress.ftl +++ b/src/main/resources/web/compress.ftl @@ -55,9 +55,22 @@ }, onClick:function (event, treeId, treeNode) { if (!treeNode.directory) { - var winHeight = window.document.documentElement.clientHeight-10; - window.open(env_config.server_preview_url + encodeURIComponent(env_config.server_base_url + treeNode.fileName) + "&needEncode=1", - "_blank", "height=" + winHeight + ",top=80,left=80,toolbar=no, menubar=no, scrollbars=yes, resizable=yes"); + /**实现窗口最大化**/ + var fulls = "left=0,screenX=0,top=0,screenY=0,scrollbars=1"; //定义弹出窗口的参数 + if (window.screen) { + var ah = screen.availHeight - 30; + var aw = (screen.availWidth - 10) / 2; + fulls += ",height=" + ah; + fulls += ",innerHeight=" + ah; + fulls += ",width=" + aw; + fulls += ",innerWidth=" + aw; + fulls += ",resizable" + } else { + fulls += ",resizable"; // 对于不支持screen属性的浏览器,可以手工进行最大化。 manually + } + window.open(env_config.server_preview_url + + encodeURIComponent(env_config.server_base_url + treeNode.fileName) + + "&needEncode=1", "_blank",fulls); } } } diff --git a/src/main/resources/web/picture.ftl b/src/main/resources/web/picture.ftl index bc5f9d55..7c707d17 100644 --- a/src/main/resources/web/picture.ftl +++ b/src/main/resources/web/picture.ftl @@ -15,8 +15,7 @@

如果图片质量很好,首次加载图片时间可能会有点长,请耐心等待

    -
  • - +