From 3e2d1ca9a9e95f247552b407801f07de28081a46 Mon Sep 17 00:00:00 2001 From: xueqingkun Date: Sun, 4 Feb 2024 10:36:57 +0800 Subject: [PATCH] =?UTF-8?q?manage:=201.fix=E4=B8=8B=E8=BD=BD=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E4=B8=AD=EF=BC=8C=E6=96=87=E4=BB=B6=E5=90=8D=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E4=B9=B1=E7=A0=81=202.=20fix=20=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E9=94=99=E8=AF=AFexcel=E6=96=87=E4=BB=B6=EF=BC=8C=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E4=B8=8D=E6=98=AFexcel=E5=90=8E=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/FileManageServiceImpl.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java b/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java index 2cf30c39..12540c52 100644 --- a/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java +++ b/virtual-patient-manage/src/main/java/com/supervision/manage/service/impl/FileManageServiceImpl.java @@ -1,5 +1,6 @@ package com.supervision.manage.service.impl; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; @@ -18,6 +19,8 @@ import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; +import java.net.URLEncoder; +import java.util.List; import java.util.Objects; @Slf4j @@ -78,11 +81,12 @@ public class FileManageServiceImpl implements FileManageService { try (InputStream inputStream = MinioUtil.download(fileResource.getMinioId()); ServletOutputStream outputStream = response.getOutputStream()){ - if (StrUtil.isNotEmpty(fileResource.getFileType())){ - response.setContentType(fileResource.getFileType()); - } + String contentType = StrUtil.isEmpty(fileResource.getFileType()) ? "application/octet-stream" : fileResource.getFileType(); + response.setContentType(contentType); + if (Objects.nonNull(fileResource.getFileName())){ - response.setHeader("Content-Disposition", "attachment;filename=" + fileResource.getFileName()); + response.setHeader("Content-Disposition", "attachment;filename=" + + URLEncoder.encode(fileResource.getFileName(), "UTF-8")); } byte[] bytes = new byte[1024]; @@ -101,7 +105,16 @@ public class FileManageServiceImpl implements FileManageService { FileResource fileResource = fileResourceService.getById(fileId); Assert.notNull(fileResource,"文件不存在"); - File tempFile = FileUtil.createTempFile(); + String prefix = null; + String suffix = null; + if (StrUtil.isNotEmpty(fileResource.getFileName())){ + List split = StrUtil.split(fileResource.getFileName(), "."); + prefix = CollUtil.getFirst(split); + if (CollUtil.size(split) > 1){ + suffix = "." + split.get(1); + } + } + File tempFile = StrUtil.isEmpty(prefix) ? FileUtil.createTempFile() : FileUtil.createTempFile(prefix,suffix,true); try (InputStream inputStream = MinioUtil.download(fileResource.getMinioId()); FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) { byte[] bytes = new byte[1024];