优化、精简代码。使用 @GetMapping、@PostMapping 替换 @RequestMapping
parent
6efe15788c
commit
b099d52520
|
@ -48,8 +48,7 @@ public class SimTextFilePreviewImpl implements FilePreview {
|
|||
private String textData(String baseUrll) throws IOException {
|
||||
File file = new File(baseUrll);
|
||||
if(!file.exists() || file.length() == 0) {
|
||||
String line="";
|
||||
return line;
|
||||
return "";
|
||||
}else {
|
||||
String charset = EncodingDetects.getJavaEncode(baseUrll);
|
||||
System.out.println(charset);
|
||||
|
|
|
@ -8,6 +8,8 @@ import cn.keking.utils.ConvertPicUtil;
|
|||
import cn.keking.utils.DownloadUtils;
|
||||
import cn.keking.utils.WebUtils;
|
||||
import cn.keking.web.filter.BaseUrlFilter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
@ -18,12 +20,15 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* tiff 图片文件处理
|
||||
*
|
||||
* @author kl (http://kailing.pub)
|
||||
* @since 2021/2/8
|
||||
*/
|
||||
@Service
|
||||
public class TiffFilePreviewImpl implements FilePreview {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(TiffFilePreviewImpl.class);
|
||||
|
||||
private final PictureFilePreviewImpl pictureFilePreview;
|
||||
private static final String INITIALIZE_MEMORY_SIZE = "initializeMemorySize";
|
||||
//默认初始化 50MB 内存
|
||||
|
@ -78,6 +83,7 @@ public class TiffFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
|
||||
// 如果pdf已经存在,则将url路径加入到对象中,返回给页面
|
||||
assert filePdf != null;
|
||||
if (filePdf.exists()) {
|
||||
String pdfUrl = baseUrl + inputFileNamePrefix + ".pdf";
|
||||
model.addAttribute("pdfUrl", pdfUrl);
|
||||
|
@ -101,8 +107,8 @@ public class TiffFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
|
||||
// 转换后的tif没用了,可以删掉了
|
||||
if(fileTiff.exists()){
|
||||
fileTiff.delete();
|
||||
if (fileTiff.exists() && !fileTiff.delete()) {
|
||||
logger.error("{} 清理失败", strLocalTif);
|
||||
}
|
||||
|
||||
return PICTURE_FILE_PREVIEW_PAGE;
|
||||
|
|
|
@ -27,6 +27,7 @@ public class ConvertPicUtil {
|
|||
|
||||
/**
|
||||
* Tif 转 JPG。
|
||||
*
|
||||
* @param strInputFile 输入文件的路径和文件名
|
||||
* @param strOutputFile 输出文件的路径和文件名
|
||||
* @return boolean 是否转换成功
|
||||
|
@ -60,8 +61,8 @@ public class ConvertPicUtil {
|
|||
int intTifCount = imageDecoder.getNumPages();
|
||||
logger.info("该tif文件共有【" + intTifCount + "】页");
|
||||
|
||||
String strJpgPath = "";
|
||||
String strJpgUrl = "";
|
||||
String strJpgPath;
|
||||
String strJpgUrl;
|
||||
if (intTifCount == 1) {
|
||||
// 如果是单页tif文件,则转换的目标文件夹就在指定的位置
|
||||
strJpgPath = strOutputFile.substring(0, strOutputFile.lastIndexOf("/"));
|
||||
|
@ -72,13 +73,13 @@ public class ConvertPicUtil {
|
|||
|
||||
// 处理目标文件夹,如果不存在则自动创建
|
||||
File fileJpgPath = new File(strJpgPath);
|
||||
if (!fileJpgPath.exists()) {
|
||||
fileJpgPath.mkdirs();
|
||||
if (!fileJpgPath.exists() && !fileJpgPath.mkdirs()) {
|
||||
logger.error("{} 创建失败", strJpgPath);
|
||||
}
|
||||
|
||||
// 循环,处理每页tif文件,转换为jpg
|
||||
for (int i = 0; i < intTifCount; i++) {
|
||||
String strJpg = "";
|
||||
String strJpg;
|
||||
if (intTifCount == 1) {
|
||||
strJpg = strJpgPath + "/" + strFilePrefix + ".jpg";
|
||||
strJpgUrl = strFilePrefix + ".jpg";
|
||||
|
@ -118,21 +119,19 @@ public class ConvertPicUtil {
|
|||
try {
|
||||
fileSeekStream.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
fileSeekStream = null;
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将Jpg图片转换为Pdf文件
|
||||
*
|
||||
* @param strJpgFile 输入的jpg的路径和文件名
|
||||
* @param strPdfFile 输出的pdf的路径和文件名
|
||||
* @return
|
||||
*/
|
||||
public static File convertJpg2Pdf(String strJpgFile, String strPdfFile) {
|
||||
public static void convertJpg2Pdf(String strJpgFile, String strPdfFile) {
|
||||
Document document = new Document();
|
||||
// 设置文档页边距
|
||||
document.setMargins(0, 0, 0, 0);
|
||||
|
@ -157,23 +156,18 @@ public class ConvertPicUtil {
|
|||
document.add(image);
|
||||
} catch (Exception ioe) {
|
||||
ioe.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
//关闭文档
|
||||
document.close();
|
||||
try {
|
||||
assert fos != null;
|
||||
fos.flush();
|
||||
fos.close();
|
||||
|
||||
File filePDF = new File(strPdfFile);
|
||||
return filePDF;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
@ -182,7 +176,7 @@ public class ConvertPicUtil {
|
|||
*
|
||||
* @param strTifFile 输入的tif的路径和文件名
|
||||
* @param strPdfFile 输出的pdf的路径和文件名
|
||||
* @return
|
||||
* @return File
|
||||
*/
|
||||
public static File convertTif2Pdf(String strTifFile, String strPdfFile) {
|
||||
try {
|
||||
|
@ -204,7 +198,7 @@ public class ConvertPicUtil {
|
|||
List<String> listPic2Jpg = convertTif2Jpg(strTifFile, strJpg);
|
||||
|
||||
if (listPic2Jpg != null && fileJpg.exists()) {
|
||||
filePDF = convertJpg2Pdf(strJpg, strPdfFile);
|
||||
convertJpg2Pdf(strJpg, strPdfFile);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@ -229,7 +223,7 @@ public class ConvertPicUtil {
|
|||
|
||||
return filePDF;
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.toString());
|
||||
logger.error(e.getMessage(), e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -1,26 +1,33 @@
|
|||
package cn.keking.web.controller;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author yudian-it
|
||||
* @date 2017/12/1
|
||||
*/
|
||||
|
@ -33,7 +40,7 @@ public class FileController {
|
|||
private final String demoDir = "demo";
|
||||
private final String demoPath = demoDir + File.separator;
|
||||
|
||||
@RequestMapping(value = "fileUpload", method = RequestMethod.POST)
|
||||
@PostMapping("/fileUpload")
|
||||
public String fileUpload(@RequestParam("file") MultipartFile file) throws JsonProcessingException {
|
||||
if (ConfigConstants.getFileUploadDisable()) {
|
||||
return new ObjectMapper().writeValueAsString(ReturnResponse.failure("文件传接口已禁用"));
|
||||
|
@ -43,6 +50,7 @@ public class FileController {
|
|||
//判断是否为IE浏览器的文件名,IE浏览器下文件名会带有盘符信息
|
||||
|
||||
// escaping dangerous characters to prevent XSS
|
||||
assert fileName != null;
|
||||
fileName = HtmlUtils.htmlEscape(fileName, StandardCharsets.UTF_8.name());
|
||||
|
||||
// Check for Unix-style path
|
||||
|
@ -72,7 +80,7 @@ public class FileController {
|
|||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "deleteFile", method = RequestMethod.GET)
|
||||
@GetMapping("/deleteFile")
|
||||
public String deleteFile(String fileName) throws JsonProcessingException {
|
||||
if (fileName.contains("/")) {
|
||||
fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
|
||||
|
@ -85,7 +93,7 @@ public class FileController {
|
|||
return new ObjectMapper().writeValueAsString(ReturnResponse.success());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "listFiles", method = RequestMethod.GET)
|
||||
@GetMapping("/listFiles")
|
||||
public String getFiles() throws JsonProcessingException {
|
||||
List<Map<String, String>> list = new ArrayList<>();
|
||||
File file = new File(fileDir + demoPath);
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
package cn.keking.web.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* 页面跳转
|
||||
|
@ -12,12 +11,12 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||
@Controller
|
||||
public class IndexController {
|
||||
|
||||
@RequestMapping(value = "/index", method = RequestMethod.GET)
|
||||
@GetMapping( "/index")
|
||||
public String go2Index(){
|
||||
return "index";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET)
|
||||
@GetMapping( "/")
|
||||
public String root() {
|
||||
return "redirect:/index";
|
||||
}
|
||||
|
|
|
@ -16,8 +16,7 @@ import org.slf4j.LoggerFactory;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.util.HtmlUtils;
|
||||
|
||||
|
@ -53,7 +52,7 @@ public class OnlinePreviewController {
|
|||
this.otherFilePreview = otherFilePreview;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/onlinePreview")
|
||||
@GetMapping( "/onlinePreview")
|
||||
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
||||
String fileUrl;
|
||||
try {
|
||||
|
@ -69,7 +68,7 @@ public class OnlinePreviewController {
|
|||
return filePreview.filePreviewHandle(fileUrl, model, fileAttribute);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/picturesPreview")
|
||||
@GetMapping( "/picturesPreview")
|
||||
public String picturesPreview(String urls, Model model, HttpServletRequest req) throws UnsupportedEncodingException {
|
||||
String fileUrls;
|
||||
try {
|
||||
|
@ -104,15 +103,16 @@ public class OnlinePreviewController {
|
|||
* @param urlPath url
|
||||
* @param response response
|
||||
*/
|
||||
@RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
|
||||
@GetMapping("/getCorsFile")
|
||||
public void getCorsFile(String urlPath, HttpServletResponse response) {
|
||||
try {
|
||||
urlPath = new String(Base64.decodeBase64(urlPath), StandardCharsets.UTF_8);
|
||||
} catch (Exception ex) {
|
||||
logger.error(String.format(BASE64_DECODE_ERROR_MSG, urlPath, ex));
|
||||
logger.error(String.format(BASE64_DECODE_ERROR_MSG, urlPath),ex);
|
||||
return;
|
||||
}
|
||||
if (urlPath == null || urlPath.toLowerCase().startsWith("file:") || urlPath.toLowerCase().startsWith("file%3") || !urlPath.toLowerCase().startsWith("http")) {
|
||||
if (urlPath.toLowerCase().startsWith("file:") || urlPath.toLowerCase().startsWith("file%3")
|
||||
|| !urlPath.toLowerCase().startsWith("http")) {
|
||||
logger.info("读取跨域文件异常,可能存在非法访问,urlPath:{}", urlPath);
|
||||
return;
|
||||
}
|
||||
|
@ -132,7 +132,7 @@ public class OnlinePreviewController {
|
|||
*
|
||||
* @param url 请编码后在入队
|
||||
*/
|
||||
@RequestMapping("/addTask")
|
||||
@GetMapping("/addTask")
|
||||
@ResponseBody
|
||||
public String addQueueTask(String url) {
|
||||
logger.info("添加转码队列url:{}", url);
|
||||
|
|
|
@ -2,16 +2,16 @@ package cn.keking.web.filter;
|
|||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.utils.WebUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.util.Base64Utils;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import javax.servlet.*;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* @author chenjh
|
||||
|
|
Loading…
Reference in New Issue