文件预览:xi修复各分支不一致的问题

This commit is contained in:
HappyTree
2017-12-30 22:04:17 +08:00
committed by klboke
parent 3a9055ce35
commit eb3d36e5fd
7 changed files with 52 additions and 230 deletions

View File

@@ -1,22 +1,15 @@
package com.yudianbank.web.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -28,6 +21,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.Arrays;
/**
* @author yudian-it
@@ -42,6 +36,10 @@ public class OnlinePreviewController {
DownloadUtils downloadUtils;
@Autowired
ZipReader zipReader;
@Autowired
SimTextUtil simTextUtil;
@Value("${simText}")
String[] simText;
@Value("${file.dir}")
String fileDir;
@@ -54,31 +52,31 @@ public class OnlinePreviewController {
@RequestMapping(value = "onlinePreview",method = RequestMethod.GET)
public String onlinePreview(String url, String needEncode, Model model, HttpServletRequest req) 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";
@@ -104,7 +102,6 @@ public class OnlinePreviewController {
return "fileNotSupported";
}
} else if ("office".equalsIgnoreCase(type)) {
String fileName = fileUtils.getFileNameFromURL(url);
boolean isHtml = suffix.equalsIgnoreCase("xls")
|| suffix.equalsIgnoreCase("xlsx");
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
@@ -112,7 +109,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";
@@ -168,6 +165,9 @@ public class OnlinePreviewController {
if (fileUtils.listOfficeTypes().contains(fileType.toLowerCase())) {
fileType = "office";
}
if (Arrays.asList(simText).contains(fileType.toLowerCase())) {
fileType = "simText";
}
return fileType;
}