From 5b1c22f95efed809051f4d6cbe0d53333c159d7e Mon Sep 17 00:00:00 2001 From: shijie <648688341@qq.com> Date: Wed, 31 Mar 2021 15:45:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DgApplyAmountInfoController.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/dq-financial-guarantee/src/main/java/com/daqing/financial/guarantee/controller/DgApplyAmountInfoController.java b/dq-financial-guarantee/src/main/java/com/daqing/financial/guarantee/controller/DgApplyAmountInfoController.java index 52f81621..af4f6d20 100644 --- a/dq-financial-guarantee/src/main/java/com/daqing/financial/guarantee/controller/DgApplyAmountInfoController.java +++ b/dq-financial-guarantee/src/main/java/com/daqing/financial/guarantee/controller/DgApplyAmountInfoController.java @@ -37,8 +37,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.*; /** @@ -144,6 +143,38 @@ public class DgApplyAmountInfoController implements DgApplyAmountInfoControllerA return ResponseResult.SUCCESS(list); } + /** + * 下载单个文件 + * + * @param response + * @param fileName + * @return + */ + @RequestMapping("/download") + public String download(HttpServletResponse response, @RequestParam("fileName") String fileName) { + File file = new File(PromptSuccess.FILE_URL_PATH + fileName); + if (file.exists()) { + response.setContentType("application/force-download;charset=UTF-8"); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Disposition", "attachment;fileName =" + fileName); + byte[] buffer = new byte[1024]; + + try (FileInputStream inputStream = new FileInputStream(file); + OutputStream outStream = response.getOutputStream(); + BufferedInputStream buffStream = new BufferedInputStream(inputStream);) { + int i = 0; + while ((i = buffStream.read(buffer)) != -1) { + outStream.write(buffer); + outStream.write(buffer, 0, i); + } + return "download success"; + } catch (Exception e) { + log.error("download error {}", e.getMessage()); + } + } + return null; + } + /** * 删除服务上的文件 * @param filePath 路径