From cee2b0bdca3016c28850fd677df5ae9236a871b2 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Mon, 26 Aug 2019 16:05:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=89=BE=E4=B8=8D=E5=88=B0=E6=96=87=E4=BB=B6=E9=98=BB?= =?UTF-8?q?=E5=A1=9E=E7=BA=BF=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/routes/file/FileRouteController.java | 3 ++- .../cm/common/plugin/service/file/IFileService.java | 5 +++-- .../plugin/service/file/impl/FileServiceImpl.java | 10 +++++----- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/routes/file/FileRouteController.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/routes/file/FileRouteController.java index 192ddba..05303e3 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/routes/file/FileRouteController.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/controller/routes/file/FileRouteController.java @@ -3,6 +3,7 @@ package com.cm.common.plugin.controller.routes.file; import com.cm.common.base.AbstractController; import com.cm.common.config.properties.FileProperties; import com.cm.common.constants.ISystemConstant; +import com.cm.common.exception.FileException; import com.cm.common.exception.ParamsException; import com.cm.common.exception.SearchException; import com.cm.common.plugin.service.file.IFileService; @@ -100,7 +101,7 @@ public class FileRouteController extends AbstractController { }) @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) @GetMapping("downloadfile/{isOpen}/{fileId}") - public void downLoadFile(HttpServletResponse response, @PathVariable("isOpen") String isOpen, @PathVariable("fileId") String fileId) throws ParamsException, SearchException { + public void downLoadFile(HttpServletResponse response, @PathVariable("isOpen") String isOpen, @PathVariable("fileId") String fileId) throws FileException, ParamsException { if (!ISystemConstant.IS_TRUE.equals(isOpen) && !ISystemConstant.IS_FALSE.equals(isOpen)) { throw new ParamsException("参数错误"); } diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java index 8a11141..2ec388b 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/IFileService.java @@ -2,6 +2,7 @@ package com.cm.common.plugin.service.file; import com.alibaba.fastjson.JSONObject; import com.cm.common.enums.UploadTypeEnum; +import com.cm.common.exception.FileException; import com.cm.common.exception.SearchException; import com.cm.common.exception.base.SystemException; import com.cm.common.pojo.dtos.FileDTO; @@ -113,9 +114,9 @@ public interface IFileService { * * @param response * @param params - * @throws SearchException + * @throws FileException */ - void downLoadFile(HttpServletResponse response, Map params) throws SearchException; + void downLoadFile(HttpServletResponse response, Map params) throws FileException; /** * 百度富文本编辑器 diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java index 24f774f..20f4f1b 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java @@ -120,7 +120,7 @@ public class FileServiceImpl extends AbstractService implements IFileService { } @Override - public void downLoadFile(HttpServletResponse response, Map params) throws SearchException { + public void downLoadFile(HttpServletResponse response, Map params) throws FileException { OutputStream outputStream = null; FileInputStream fileInputStream = null; try { @@ -128,19 +128,20 @@ public class FileServiceImpl extends AbstractService implements IFileService { if (null == filePO) { throw new SearchException("文件获取失败"); } + fileInputStream = new FileInputStream(new File(filePO.getFilePath())); response.setContentType(getContentType(filePO.getFileType())); - response.addHeader("Content-Length", filePO.getFileSize()); + response.setHeader("Content-Length", filePO.getFileSize()); if (!Boolean.valueOf(params.get("isOpen").toString())) { - response.addHeader("Content-Disposition", "attachment;fileName=" + filePO.getFileName()); + response.setHeader("Content-Disposition", "attachment;fileName=" + filePO.getFileName()); } outputStream = response.getOutputStream(); - fileInputStream = new FileInputStream(new File(filePO.getFilePath())); for (byte[] buf = new byte[INPUT_STREAM_SIZE]; fileInputStream.read(buf) > -1; ) { outputStream.write(buf, 0, buf.length); } outputStream.flush(); } catch (Exception e) { LOG.error(e.getMessage(), e); + throw new FileException("文件下载异常"); } finally { try { if (null != outputStream) { @@ -152,7 +153,6 @@ public class FileServiceImpl extends AbstractService implements IFileService { } catch (Exception e1) { LOG.error(e1.getMessage(), e1); } - throw new SearchException("文件获取失败"); } }