优化:重构大量代码,修复异常

This commit is contained in:
陈精华
2020-05-15 18:09:19 +08:00
committed by kl
parent 8a52450629
commit 180e7bcb8a
35 changed files with 464 additions and 699 deletions

View File

@@ -1,17 +1,15 @@
package cn.keking.web.controller;
import cn.keking.config.ConfigConstants;
import cn.keking.hutool.URLUtil;
import cn.keking.model.FileAttribute;
import cn.keking.service.FilePreview;
import cn.keking.service.FilePreviewFactory;
import cn.keking.service.cache.CacheService;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@@ -22,7 +20,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.*;
import java.util.Arrays;
import java.util.List;
@@ -34,20 +31,25 @@ public class OnlinePreviewController {
private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);
@Autowired
private FilePreviewFactory previewFactory;
private final FilePreviewFactory previewFactory;
@Autowired
private CacheService cacheService;
private final CacheService cacheService;
private final FileUtils fileUtils;
private final DownloadUtils downloadUtils;
public OnlinePreviewController(FilePreviewFactory filePreviewFactory,
FileUtils fileUtils,
CacheService cacheService,
DownloadUtils downloadUtils) {
this.previewFactory = filePreviewFactory;
this.fileUtils = fileUtils;
this.cacheService = cacheService;
this.downloadUtils = downloadUtils;
}
@Autowired
private FileUtils fileUtils;
/**
* @param url
* @param model
* @return
*/
@RequestMapping(value = "/onlinePreview", method = RequestMethod.GET)
public String onlinePreview(String url, Model model, HttpServletRequest req) {
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
@@ -66,7 +68,7 @@ public class OnlinePreviewController {
String currentUrl = req.getParameter("currentUrl");
logger.info("预览文件url{}urls{}", currentUrl, urls);
String[] imgs = urls.split("\\|");
List imgurls = Arrays.asList(imgs);
List<String> imgurls = Arrays.asList(imgs);
model.addAttribute("imgurls", imgurls);
model.addAttribute("currentUrl", currentUrl);
return "picture";
@@ -75,31 +77,16 @@ public class OnlinePreviewController {
* 根据url获取文件内容
* 当pdfjs读取存在跨域问题的文件时将通过此接口读取
*
* @param urlPath
* @param resp
* @param urlPath url
* @param response response
*/
@RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
public void getCorsFile(String urlPath, HttpServletResponse resp) {
InputStream inputStream = null;
public void getCorsFile(String urlPath, HttpServletResponse response) {
logger.info("下载跨域pdf文件url{}", urlPath);
try {
URL url = new URL(URLUtil.normalize(urlPath, true));
//打开请求连接
URLConnection connection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
inputStream = httpURLConnection.getInputStream();
byte[] bs = new byte[1024];
int len;
while (-1 != (len = inputStream.read(bs))) {
resp.getOutputStream().write(bs, 0, len);
}
downloadUtils.saveToOutputStreamFormUrl(urlPath, response.getOutputStream());
} catch (IOException e) {
logger.error("下载跨域pdf文件异常url{}", urlPath, e);
} finally {
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
}
}