diff --git a/huzhou/src/main/java/com/easy/admin/modules/huzhou/controller/HuzhouUploadfileinfoController.java b/huzhou/src/main/java/com/easy/admin/modules/huzhou/controller/HuzhouUploadfileinfoController.java index 7c7f380..1270e39 100644 --- a/huzhou/src/main/java/com/easy/admin/modules/huzhou/controller/HuzhouUploadfileinfoController.java +++ b/huzhou/src/main/java/com/easy/admin/modules/huzhou/controller/HuzhouUploadfileinfoController.java @@ -26,7 +26,11 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.WritableByteChannel; import java.util.ArrayList; +import java.util.HashMap; @RestController @RequestMapping("/huzhouUploadfileinfo") @@ -49,35 +53,54 @@ public class HuzhouUploadfileinfoController { path = "excelTemplate"+File.separator+path.substring(path.indexOf("/")+1); ClassPathResource classPathResource = new ClassPathResource(path); fileInputStream =classPathResource.getInputStream(); - }else{ - fileInputStream = new FileInputStream(path); - file = new File(path); - } - response.addHeader("content-disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8")+";filename*=UTF-8''"+ URLEncoder.encode(fileName,"UTF-8")); - response.setContentType("application/octet-stream"); - if(file!=null){ - response.addHeader("Content-Length", "" + file.length()); - }else{ - response.addHeader("Content-Length", "" + fileInputStream.available()); - } + response.addHeader("content-disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8")+";filename*=UTF-8''"+ URLEncoder.encode(fileName,"UTF-8")); + response.setContentType("application/octet-stream"); + if(file!=null){ + response.addHeader("Content-Length", "" + file.length()); + }else{ + response.addHeader("Content-Length", "" + fileInputStream.available()); + } // response.setContentType("multipart/form-data"); - BufferedInputStream inputStream =null; - try { - ServletOutputStream outputStream = response.getOutputStream(); - inputStream = new BufferedInputStream(fileInputStream); - byte[] bytes = new byte[1024]; - int len =inputStream.read(bytes); - while (len!=-1){ - outputStream.write(bytes,0,bytes.length); - outputStream.flush(); - len=inputStream.read(bytes); + BufferedInputStream inputStream =null; + try { + ServletOutputStream outputStream = response.getOutputStream(); + inputStream = new BufferedInputStream(fileInputStream); + byte[] bytes = new byte[1024]; + int len =inputStream.read(bytes); + while (len!=-1){ + outputStream.write(bytes,0,bytes.length); + outputStream.flush(); + len=inputStream.read(bytes); + } + }finally { + if(inputStream!=null){ + inputStream.close(); + } } - }finally { - if(inputStream!=null){ - inputStream.close(); + }else{ + file = new File(path); + try { + + // 设置响应头 + response.setHeader("content-disposition", "attachment;filename=" + + URLEncoder.encode(fileName, "UTF-8") + + ";filename*=UTF-8''" + URLEncoder.encode(fileName, "UTF-8")); + response.setContentType("application/octet-stream"); + response.setContentLength((int) file.length()); + + // 使用NIO通道 + try (FileChannel fileChannel = new FileInputStream(file).getChannel(); + WritableByteChannel outputChannel = Channels.newChannel(response.getOutputStream())) { + // 使用transferTo方法直接传输数据 + fileChannel.transferTo(0, file.length(), outputChannel); + } + } catch (Exception e) { + throw new RuntimeException("文件下载失败", e); } } + + } @GetMapping("/uploacFilePageList")