Browse Source

添加properties文件预览支持

pull/1/head
woshiduanmu 7 years ago committed by klboke
parent
commit
5d18e447fd
  1. 4
      src/main/java/com/yudianbank/utils/DownloadUtils.java
  2. 18
      src/main/java/com/yudianbank/utils/OfficeToPdf.java
  3. 2
      src/main/java/com/yudianbank/utils/ShedulerClean.java
  4. 24
      src/main/java/com/yudianbank/utils/SimTextUtil.java
  5. 43
      src/main/java/com/yudianbank/web/controller/OnlinePreviewController.java
  6. 4
      src/main/resources/application.properties
  7. 19
      src/main/resources/web/compress.ftl
  8. 4
      src/main/resources/web/picture.ftl

4
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();

18
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;
}*/
/**
* 转换文件
*

2
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);

24
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<String> readSimText(String url, String fileName, String needEncode){
ReturnResponse<String> response = downloadUtils.downLoad(url, "txt", fileName, needEncode);
return response;
}
}

43
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<String> 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<String> response = downloadUtils.downLoad(url, suffix, fileName, needEncode);
ReturnResponse<String> 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<String> response = downloadUtils.downLoad(url, suffix, null, needEncode);
ReturnResponse<String> 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;
}

4
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
spring.http.multipart.max-file-size=100MB
## 支持的类文本格式的文件类型
simText = txt,html,xml,java,properties,mp3,mp4,sql

19
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);
}
}
}

4
src/main/resources/web/picture.ftl

@ -15,8 +15,7 @@
<h1>如果图片质量很好,首次加载图片时间可能会有点长,请耐心等待</h1>
<ul id="dowebok">
<li><img id="Imgbox" src="#" width="800px" height="550px"></li>
<li><img id="Imgbox" src="#" width="800px" height="auto"></li>
</ul>
<script src="js/viewer.min.js"></script>
<script>
@ -26,6 +25,7 @@
document.getElementById("Imgbox").src =document.getElementById("url").value;
}
var viewer = new Viewer(document.getElementById('dowebok'), {url: 'src'});
viewer.show();
</script>
<input name="url" value="${imgurl}" type="hidden" id="url" >
</body>

Loading…
Cancel
Save