添加properties文件预览支持

pull/1/head
woshiduanmu 2017-12-13 18:45:39 +08:00 committed by klboke
parent 1dfc1f678f
commit 5d18e447fd
8 changed files with 74 additions and 44 deletions

View File

@ -48,6 +48,8 @@ public class DownloadUtils {
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
if (null == fileName) { if (null == fileName) {
fileName = uuid+ "."+type; fileName = uuid+ "."+type;
}else { // 文件后缀不一致时以type为准(针对simText【将类txt文件转为txt】)
fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
} }
String realPath = fileDir + fileName; String realPath = fileDir + fileName;
File dirFile = new File(fileDir); File dirFile = new File(fileDir);
@ -67,6 +69,8 @@ public class DownloadUtils {
os.close(); os.close();
in.close(); in.close();
response.setContent(realPath); response.setContent(realPath);
// 同样针对类txt文件如果成功msg包含的是转换后的文件名
response.setMsg(fileName);
return response; return response;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -22,24 +22,6 @@ public class OfficeToPdf {
return office2pdf(inputFilePath, outputFilePath); 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;
}*/
/** /**
* *
* *

View File

@ -11,7 +11,7 @@ public class ShedulerClean {
@Value("${file.dir}") @Value("${file.dir}")
String fileDir; String fileDir;
@Scheduled(cron = "0 0 23 * * ?") //每晚23点执行一次 // @Scheduled(cron = "0 0 23 * * ?") //每晚23点执行一次
public void clean(){ public void clean(){
System.out.println("执行一次清空文件夹"); System.out.println("执行一次清空文件夹");
DeleteFileUtil.deleteDirectory(fileDir); DeleteFileUtil.deleteDirectory(fileDir);

View File

@ -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;
}
}

View File

@ -1,12 +1,9 @@
package com.yudianbank.web.controller; package com.yudianbank.web.controller;
import com.fasterxml.jackson.core.JsonProcessingException; import com.ctrip.framework.apollo.Config;
import com.fasterxml.jackson.databind.ObjectMapper; import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.yudianbank.param.ReturnResponse; import com.yudianbank.param.ReturnResponse;
import com.yudianbank.utils.DownloadUtils; import com.yudianbank.utils.*;
import com.yudianbank.utils.FileUtils;
import com.yudianbank.utils.OfficeToPdf;
import com.yudianbank.utils.ZipReader;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.Arrays;
/** /**
* @author yudian-it * @author yudian-it
@ -33,6 +30,10 @@ public class OnlinePreviewController {
DownloadUtils downloadUtils; DownloadUtils downloadUtils;
@Autowired @Autowired
ZipReader zipReader; ZipReader zipReader;
@Autowired
SimTextUtil simTextUtil;
@ApolloConfig
Config config;
@Value("${file.dir}") @Value("${file.dir}")
String fileDir; String fileDir;
@ -49,31 +50,32 @@ public class OnlinePreviewController {
@RequestMapping(value = "onlinePreview",method = RequestMethod.GET) @RequestMapping(value = "onlinePreview",method = RequestMethod.GET)
public String onlinePreview(String url, String needEncode, Model model) throws UnsupportedEncodingException { 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 type = typeFromUrl(url);
String suffix = suffixFromUrl(url); String suffix = suffixFromUrl(url);
// 抽取文件并返回文件列表
String fileName = fileUtils.getFileNameFromURL(decodedUrl);
model.addAttribute("fileType", suffix); model.addAttribute("fileType", suffix);
if (type.equalsIgnoreCase("picture")) { if (type.equalsIgnoreCase("picture")) {
model.addAttribute("imgurl", url); model.addAttribute("imgurl", url);
return "picture"; return "picture";
} else if (type.equalsIgnoreCase("txt") } else if (type.equalsIgnoreCase("simText")){
|| type.equalsIgnoreCase("html") ReturnResponse<String> response = simTextUtil.readSimText(decodedUrl, fileName, needEncode);
|| type.equalsIgnoreCase("xml") if (0 != response.getCode()) {
|| type.equalsIgnoreCase("java") model.addAttribute("msg", response.getMsg());
|| type.equalsIgnoreCase("properties") return "fileNotSupported";
|| type.equalsIgnoreCase("mp3")){ }
model.addAttribute("ordinaryUrl",url); model.addAttribute("ordinaryUrl", response.getMsg());
return "txt"; return "txt";
} else if(type.equalsIgnoreCase("pdf")){ } else if(type.equalsIgnoreCase("pdf")){
model.addAttribute("pdfUrl",url); model.addAttribute("pdfUrl",url);
return "pdf"; return "pdf";
} else if(type.equalsIgnoreCase("compress")){ } else if(type.equalsIgnoreCase("compress")){
// 抽取文件并返回文件列表 // 抽取文件并返回文件列表
String fileName = fileUtils.getFileNameFromURL(url);
String fileTree = null; String fileTree = null;
// 判断文件名是否存在(redis缓存读取) // 判断文件名是否存在(redis缓存读取)
if (!StringUtils.hasText(fileUtils.getConvertedFile(fileName))) { 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()) { if (0 != response.getCode()) {
model.addAttribute("msg", response.getMsg()); model.addAttribute("msg", response.getMsg());
return "fileNotSupported"; return "fileNotSupported";
@ -99,7 +101,6 @@ public class OnlinePreviewController {
return "fileNotSupported"; return "fileNotSupported";
} }
} else if ("office".equalsIgnoreCase(type)) { } else if ("office".equalsIgnoreCase(type)) {
String fileName = fileUtils.getFileNameFromURL(url);
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1)
+ ((suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx")) ? + ((suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx")) ?
"html" : "pdf"); "html" : "pdf");
@ -107,7 +108,7 @@ public class OnlinePreviewController {
if (!fileUtils.listConvertedFiles().containsKey(pdfName)) { if (!fileUtils.listConvertedFiles().containsKey(pdfName)) {
String filePath = fileDir + fileName; String filePath = fileDir + fileName;
if (!new File(filePath).exists()) { 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()) { if (0 != response.getCode()) {
model.addAttribute("msg", response.getMsg()); model.addAttribute("msg", response.getMsg());
return "fileNotSupported"; return "fileNotSupported";
@ -163,6 +164,10 @@ public class OnlinePreviewController {
if (fileUtils.listOfficeTypes().contains(fileType.toLowerCase())) { if (fileUtils.listOfficeTypes().contains(fileType.toLowerCase())) {
fileType = "office"; fileType = "office";
} }
if (Arrays.asList(config.getArrayProperty("simText",",",new String[]{}))
.contains(fileType.toLowerCase())) {
fileType = "simText";
}
return fileType; return fileType;
} }

View File

@ -21,3 +21,5 @@ server.tomcat.uri-encoding = UTF-8
converted.file.charset = GBK 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

View File

@ -55,9 +55,22 @@
}, },
onClick:function (event, treeId, treeNode) { onClick:function (event, treeId, treeNode) {
if (!treeNode.directory) { 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", var fulls = "left=0,screenX=0,top=0,screenY=0,scrollbars=1"; //定义弹出窗口的参数
"_blank", "height=" + winHeight + ",top=80,left=80,toolbar=no, menubar=no, scrollbars=yes, resizable=yes"); 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);
} }
} }
} }

View File

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