From 7810744afc6146ebda5060057f4dae2853ffde5a Mon Sep 17 00:00:00 2001 From: chenxiwang <1520264117@qq.com> Date: Wed, 21 Aug 2024 10:22:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=83=A8=E7=BD=B2=E5=90=8E?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/OsmoticWaterRController.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/gunshi/project/xyt/controller/OsmoticWaterRController.java b/src/main/java/com/gunshi/project/xyt/controller/OsmoticWaterRController.java index bb9128c..58f09cf 100644 --- a/src/main/java/com/gunshi/project/xyt/controller/OsmoticWaterRController.java +++ b/src/main/java/com/gunshi/project/xyt/controller/OsmoticWaterRController.java @@ -1,6 +1,5 @@ package com.gunshi.project.xyt.controller; -import cn.hutool.core.io.resource.ClassPathResource; import com.alibaba.fastjson2.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; @@ -38,21 +37,17 @@ import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; -import java.util.stream.Collectors; /** * 描述: 水质采样记录表 @@ -195,16 +190,16 @@ public class OsmoticWaterRController { @PostMapping("/downloadFile") @CrossOrigin public void downloadFile(HttpServletResponse response) throws IOException { - ClassPathResource classPathResource = new ClassPathResource("/doc/2002年6月1日_GB3838-2002地表水环境质量标准.pdf"); - File file = classPathResource.getFile(); + String filePath = "doc/2002年6月1日_GB3838-2002地表水环境质量标准.pdf"; + File file = new File(filePath); if (!file.exists()) { - new IllegalArgumentException("文件不存在!"); + new FileNotFoundException("文件不存在!"); } - response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8")); - BufferedInputStream inputStream = null; + response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filePath, "UTF-8")); + InputStream inputStream = null; OutputStream outputStream = null; try { - inputStream = new BufferedInputStream(new FileInputStream(file)); + inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath); outputStream = response.getOutputStream(); byte[] buffer = new byte[1024]; int len = 0;