From d5db4cd0dcb3a255108acb8430d0f209b57fda0a Mon Sep 17 00:00:00 2001 From: aikai Date: Fri, 22 Mar 2024 14:09:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3ios=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E6=89=93=E5=BC=80pdf=E4=B9=B1=E7=A0=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/util/servlet/ServletUtils.java | 18 ++++-- .../infra/service/file/FileServiceImpl.java | 59 ++++++++----------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java index 61038de9..1bcd6421 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/servlet/ServletUtils.java @@ -14,12 +14,11 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; +import java.nio.file.Paths; import java.util.Map; /** * 客户端工具类 - * - */ public class ServletUtils { @@ -45,9 +44,20 @@ public class ServletUtils { public static void writeAttachment(HttpServletResponse response, String filename, byte[] content) throws IOException { // 设置 header 和 contentType response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); - response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); + if (isPdfFile(filename)) { + response.setContentType("application/pdf;charset=utf-8"); + } else { + response.setContentType("application/octet-stream;charset=utf-8"); + } // 输出附件 - IoUtil.write(response.getOutputStream(), false, content); + IoUtil.write(response.getOutputStream(), true, content); + } + + public static boolean isPdfFile(String filePath) { + // 获取文件名(包括扩展名) + String fileName = Paths.get(filePath).getFileName().toString(); + // 检查扩展名是否为.pdf + return fileName.toLowerCase().endsWith(".pdf"); } /** diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index 0d381205..03cf1b99 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java @@ -16,23 +16,18 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import cn.iocoder.yudao.module.infra.dal.mysql.file.BpmFileMapper; import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; -import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import lombok.SneakyThrows; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; - import java.util.List; -import java.util.Map; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS; /** * 文件 Service 实现类 - * - */ @Service public class FileServiceImpl implements FileService { @@ -58,20 +53,18 @@ public class FileServiceImpl implements FileService { Long userId = SecurityFrameworkUtils.getLoginUserId(); long timestamp = System.currentTimeMillis(); - String[] fileInfo = new String[4] ; - String name = file.getOriginalFilename() ; - String path = null ; - byte[] content = IoUtil.readBytes(file.getInputStream()) ; + String[] fileInfo = new String[4]; + String name = file.getOriginalFilename(); + String path = null; + byte[] content = IoUtil.readBytes(file.getInputStream()); // 计算默认的 path 名 String type = FileTypeUtils.getMineType(content, name); - if (StrUtil.isEmpty(path)) { - //path = FileUtils.generatePath(content, name); - path = userId+"_"+timestamp+""; - String beginPath = name.replace(".","_") ; - path = beginPath+"_"+path+"."+ FileNameUtil.extName(name);; - } + StrUtil.isEmpty(path);//path = FileUtils.generatePath(content, name); + path = userId + "_" + timestamp; + String beginPath = name.replace(".", "_"); + path = beginPath + "_" + path + "." + FileNameUtil.extName(name); // 如果 name 为空,则使用 path 填充 if (StrUtil.isEmpty(name)) { name = path; @@ -90,15 +83,15 @@ public class FileServiceImpl implements FileService { fileDo.setUrl(url); fileDo.setType(type); fileDo.setSize(content.length); - fileDo.setUploadUserId(userId) ; + fileDo.setUploadUserId(userId); bpmFileMapper.insert(fileDo); - fileInfo[0] = name ; - fileInfo[1] = fileDo.getUrl() ; - fileInfo[2] = fileDo.getType() ; + fileInfo[0] = name; + fileInfo[1] = fileDo.getUrl(); + fileInfo[2] = fileDo.getType(); // 返回上传文件的访问路径 //return "{\"name\": \""+ fileInfo[0]+"\", \"path\": \""+ fileInfo[1]+"\", \"type\": \""+ fileInfo[2]+"\" }"; - return fileDo ; + return fileDo; } @Override @@ -162,11 +155,11 @@ public class FileServiceImpl implements FileService { @Override public void deleteFile(String url) throws Exception { - String path = url.substring( url.lastIndexOf("/")+1 ) ; - PageResult fileDOPageResult =fileMapper.selectPage(new FilePageReqVO().setPath(path)); - FileDO file = null ; - if(fileDOPageResult != null && fileDOPageResult.getTotal() >0 ) { - file = fileDOPageResult.getList().get(0) ; + String path = url.substring(url.lastIndexOf("/") + 1); + PageResult fileDOPageResult = fileMapper.selectPage(new FilePageReqVO().setPath(path)); + FileDO file = null; + if (fileDOPageResult != null && fileDOPageResult.getTotal() > 0) { + file = fileDOPageResult.getList().get(0); } if (file == null) { @@ -184,11 +177,11 @@ public class FileServiceImpl implements FileService { @Override public void deleteBpmFile(String url) throws Exception { - String path = url.substring( url.lastIndexOf("/")+1 ) ; - PageResult fileDOPageResult =bpmFileMapper.selectPage(new FilePageReqVO().setPath(path)); - BpmFileDO file = null ; - if(fileDOPageResult != null && fileDOPageResult.getTotal() >0 ) { - file = fileDOPageResult.getList().get(0) ; + String path = url.substring(url.lastIndexOf("/") + 1); + PageResult fileDOPageResult = bpmFileMapper.selectPage(new FilePageReqVO().setPath(path)); + BpmFileDO file = null; + if (fileDOPageResult != null && fileDOPageResult.getTotal() > 0) { + file = fileDOPageResult.getList().get(0); } if (file == null) { throw exception(FILE_NOT_EXISTS); @@ -205,10 +198,10 @@ public class FileServiceImpl implements FileService { @Override public void uploadBpmFileProcessInstanceId(BpmFileUploadReqVO reqVO) { - String processInstanceId = reqVO.getProcessInstanceId() ; - List urls = reqVO.getUrls() ; + String processInstanceId = reqVO.getProcessInstanceId(); + List urls = reqVO.getUrls(); LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper<>(); - lambdaUpdateWrapper.in(BpmFileDO::getUrl, urls) ; + lambdaUpdateWrapper.in(BpmFileDO::getUrl, urls); lambdaUpdateWrapper.set(BpmFileDO::getProcessInstanceId, processInstanceId); // 假设 bid 是要更新的 bid 值 // 调用 MyBatis Plus 的 update 方法执行批量更新 bpmFileMapper.update(null, lambdaUpdateWrapper);