From e25564aea2c6e73e078c5699da94be848cea32fc Mon Sep 17 00:00:00 2001 From: wangxy <1356089412@qq.com> Date: Fri, 12 Apr 2024 15:33:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=9D=90=E6=96=99=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/common/CommonController.java | 52 ++++++------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/hyp-admin/src/main/java/com/hyp/web/controller/common/CommonController.java b/hyp-admin/src/main/java/com/hyp/web/controller/common/CommonController.java index cce1292..23c4540 100644 --- a/hyp-admin/src/main/java/com/hyp/web/controller/common/CommonController.java +++ b/hyp-admin/src/main/java/com/hyp/web/controller/common/CommonController.java @@ -44,12 +44,9 @@ public class CommonController * @param delete 是否删除 */ @GetMapping("/download") - public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) - { - try - { - if (!FileUtils.checkAllowDownload(fileName)) - { + public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) { + try { + if (!FileUtils.checkAllowDownload(fileName)) { throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName)); } String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); @@ -58,13 +55,10 @@ public class CommonController response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, realFileName); FileUtils.writeBytes(filePath, response.getOutputStream()); - if (delete) - { + if (delete) { FileUtils.deleteFile(filePath); } - } - catch (Exception e) - { + } catch (Exception e) { log.error("下载文件失败", e); } } @@ -73,10 +67,8 @@ public class CommonController * 通用上传请求(单个) */ @PostMapping("/upload") - public AjaxResult uploadFile(MultipartFile file) throws Exception - { - try - { + public AjaxResult uploadFile(MultipartFile file) throws Exception { + try { // 上传文件路径 String filePath = HypConfig.getUploadPath(); // 上传并返回新文件名称 @@ -88,9 +80,7 @@ public class CommonController ajax.put("newFileName", FileUtils.getName(fileName)); ajax.put("originalFilename", file.getOriginalFilename()); return ajax; - } - catch (Exception e) - { + } catch (Exception e) { return AjaxResult.error(e.getMessage()); } } @@ -99,18 +89,15 @@ public class CommonController * 通用上传请求(多个) */ @PostMapping("/uploads") - public AjaxResult uploadFiles(List files) throws Exception - { - try - { + public AjaxResult uploadFiles(List files) throws Exception { + try { // 上传文件路径 String filePath = HypConfig.getUploadPath(); List urls = new ArrayList(); List fileNames = new ArrayList(); List newFileNames = new ArrayList(); List originalFilenames = new ArrayList(); - for (MultipartFile file : files) - { + for (MultipartFile file : files) { // 上传并返回新文件名称 String fileName = FileUploadUtils.upload(filePath, file); String url = serverConfig.getUrl() + fileName; @@ -125,9 +112,7 @@ public class CommonController ajax.put("newFileNames", StringUtils.join(newFileNames, FILE_DELIMETER)); ajax.put("originalFilenames", StringUtils.join(originalFilenames, FILE_DELIMETER)); return ajax; - } - catch (Exception e) - { + } catch (Exception e) { return AjaxResult.error(e.getMessage()); } } @@ -137,12 +122,9 @@ public class CommonController */ @GetMapping("/download/resource") public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) - throws Exception - { - try - { - if (!FileUtils.checkAllowDownload(resource)) - { + throws Exception { + try { + if (!FileUtils.checkAllowDownload(resource)) { throw new Exception(StringUtils.format("资源文件({})非法,不允许下载。 ", resource)); } // 本地资源路径 @@ -154,9 +136,7 @@ public class CommonController response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, downloadName); FileUtils.writeBytes(downloadPath, response.getOutputStream()); - } - catch (Exception e) - { + } catch (Exception e) { log.error("下载文件失败", e); } }