From c5ead63fad559185f8e29131e18ccd4bd441d8d3 Mon Sep 17 00:00:00 2001 From: yuejiayang <146344614+YangLei105@users.noreply.github.com> Date: Fri, 3 Jan 2025 21:36:45 +0800 Subject: [PATCH] =?UTF-8?q?2025.1.3=20=E9=A1=B9=E7=9B=AE=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=20=E6=96=B0=E5=A2=9E=E6=A8=A1=E6=80=81=E6=A1=86=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20=E8=AF=A2=E4=BB=B7=20=E8=AF=A2=E4=BB=B7=E5=AE=A1?= =?UTF-8?q?=E6=89=B9=20=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/oss/controller/OssController.java | 5 ++ .../modules/oss/service/SysOssService.java | 3 ++ .../oss/service/impl/SysOssServiceImpl.java | 48 ++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/xujie/sys/modules/oss/controller/OssController.java b/src/main/java/com/xujie/sys/modules/oss/controller/OssController.java index def0b05e..0ad4dfa5 100644 --- a/src/main/java/com/xujie/sys/modules/oss/controller/OssController.java +++ b/src/main/java/com/xujie/sys/modules/oss/controller/OssController.java @@ -41,4 +41,9 @@ public class OssController { public void previewOssFileById(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception { sysOssService.previewOssFileById(id,response); } + + @PostMapping("/2/{id}") + public void previewOssFileById2(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception { + sysOssService.previewOssFileById2(id,response); + } } diff --git a/src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java b/src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java index 4868f06b..74e4c28a 100644 --- a/src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java +++ b/src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java @@ -53,4 +53,7 @@ public interface SysOssService extends IService { List queryOssFile(SysOssEntity oss); void previewOssFileById(Integer id, HttpServletResponse response) throws Exception; + + void previewOssFileById2(Integer id, HttpServletResponse response) throws Exception; + } diff --git a/src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java b/src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java index abc7f8a7..f3e56c55 100644 --- a/src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java +++ b/src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java @@ -153,7 +153,53 @@ public class SysOssServiceImpl extends ServiceImpl impl } @Override - public void previewOssFileById(Integer id, HttpServletResponse response) throws Exception { + public void previewOssFileById(Integer id, HttpServletResponse response) { + SysOssEntity oss = this.getById(id); + if (Objects.isNull(oss)) { + throw new IllegalArgumentException("文件不存在"); + } + + // 创建文件对象 + File file = new File(oss.getUrl()); + if (!file.exists()) { + throw new IllegalArgumentException("文件不存在"); + } + + String encodedFileName; + try { + encodedFileName = URLEncoder.encode(oss.getFileName(), "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException("文件名编码失败", e); + } + + // 设置响应头 + if ("txt".equals(oss.getFileSuffix())){ + response.setContentType("text/plain;charset=utf-8"); + }else { + response.setContentType("application/octet-stream;charset=utf-8"); + } + response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName); + + try (FileInputStream fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis); + ServletOutputStream os = response.getOutputStream()) { + + byte[] buffer = new byte[1024]; + int bytesRead; + + // 循环读取文件内容并写入到响应流中 + while ((bytesRead = bis.read(buffer)) != -1) { + os.write(buffer, 0, bytesRead); + } + + } catch (IOException e) { + throw new RuntimeException("文件下载失败", e); + } + + } + + @Override + public void previewOssFileById2(Integer id, HttpServletResponse response) throws Exception { SysOssEntity oss = this.getById(id); if (Objects.isNull(oss)) { throw new IllegalArgumentException("文件不存在");