From 3ce787aa67ecfa93be5cb44b0210c6889200fc3d Mon Sep 17 00:00:00 2001 From: wangxy <1356089412@qq.com> Date: Fri, 12 Apr 2024 16:44:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=9D=90=E6=96=99=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/FileRelationManager.java | 44 +++++++++++ .../reward/FileRelationController.java | 73 +++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/hyp-admin/src/main/java/com/hyp/web/controller/manager/FileRelationManager.java b/hyp-admin/src/main/java/com/hyp/web/controller/manager/FileRelationManager.java index 7820aa0..a46b799 100644 --- a/hyp-admin/src/main/java/com/hyp/web/controller/manager/FileRelationManager.java +++ b/hyp-admin/src/main/java/com/hyp/web/controller/manager/FileRelationManager.java @@ -7,6 +7,8 @@ import com.hyp.common.exception.ServiceException; import com.hyp.system.domain.RewFileRelation; import com.hyp.system.service.RewFileRelationService; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + import javax.annotation.Resource; import java.util.List; import java.util.Objects; @@ -79,6 +81,48 @@ public class FileRelationManager { return AjaxResult.success(); } + @Transactional(rollbackFor = Exception.class) + public RewFileRelation save(String applyId,String filename,String fileCode,String url,String originalFilename){ + RewFileRelation rewFileRelation = new RewFileRelation(); + rewFileRelation.setApplyId(applyId); + rewFileRelation.setFileName(filename); + rewFileRelation.setFileCode(fileCode); + rewFileRelation.setFilePath(url); + rewFileRelation.setRealName(originalFilename); + fileRelationService.save(rewFileRelation); + return rewFileRelation; + } + + /** + * + * 获取文件路劲 + * @param fileId + * @return java.lang.String + */ + + public String getFilePath(String fileId){ + RewFileRelation fileRelation = fileRelationService.getById(fileId); + if(Objects.nonNull(fileRelation)){ + return fileRelation.getFilePath(); + } + return null; + } + + /** + * + * 获取文件名 + * @param fileId + * @return java.lang.String + */ + + public String getRealName(String fileId){ + RewFileRelation fileRelation = fileRelationService.getById(fileId); + if(Objects.nonNull(fileRelation)){ + return fileRelation.getRealName(); + } + return null; + } + diff --git a/hyp-admin/src/main/java/com/hyp/web/controller/reward/FileRelationController.java b/hyp-admin/src/main/java/com/hyp/web/controller/reward/FileRelationController.java index 4acde06..ac22dd7 100644 --- a/hyp-admin/src/main/java/com/hyp/web/controller/reward/FileRelationController.java +++ b/hyp-admin/src/main/java/com/hyp/web/controller/reward/FileRelationController.java @@ -1,13 +1,28 @@ package com.hyp.web.controller.reward; +import com.hyp.common.config.HypConfig; import com.hyp.common.core.controller.BaseController; import com.hyp.common.core.domain.AjaxResult; +import com.hyp.common.utils.file.FileUploadUtils; +import com.hyp.common.utils.file.FileUtils; +import com.hyp.framework.config.ServerConfig; +import com.hyp.system.domain.RewFileRelation; import com.hyp.web.controller.manager.FileRelationManager; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.util.Objects; /** @@ -22,12 +37,16 @@ import javax.annotation.Resource; @Api("文件") @RestController @RequestMapping("/reward/file") +@Slf4j public class FileRelationController extends BaseController { @Resource private FileRelationManager fileRelationManager; + @Autowired + private ServerConfig serverConfig; + @ApiOperation("材料删除") @GetMapping("/delFile/{fileId}") public AjaxResult delFile(@PathVariable String fileId){ @@ -51,7 +70,61 @@ public class FileRelationController extends BaseController { } + @ApiOperation("文件上传") + @PostMapping("/upload") + public AjaxResult uploadFile(MultipartFile file, + @RequestParam String applyId, + @RequestParam String filename, + @RequestParam String fileCode) { + try { + // 上传文件路径 + String filePath = HypConfig.getUploadPath(); + // 上传并返回新文件名称 + String fileNames = FileUploadUtils.upload(filePath, file); + String url = serverConfig.getUrl() + fileNames; + String originalFilename = FileUtils.getNameNotSuffix(file.getOriginalFilename()) ; + return AjaxResult.success(fileRelationManager.save(applyId,filename,fileCode,url,originalFilename)); + } catch (Exception e) { + return AjaxResult.error(e.getMessage()); + } + } + @ApiOperation("材料预览") + @GetMapping("fileView/{fileId}") + public void fileView(@PathVariable String fileId, HttpServletResponse response) throws IOException { + String filePath = fileRelationManager.getFilePath(fileId); + String fileName = fileRelationManager.getRealName(fileId); + String encodedFileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1"); + InputStream in = null; + OutputStream out = null; + try { + response.setHeader("content-disposition", "attachment;filename=" + encodedFileName + ".pdf"); + // 设置文件ContentType类型,这样设置,会自动判断下载文件类型 + response.setContentType("application/octet-stream;charset=utf-8"); + File file = new File(filePath); + in = Files.newInputStream(file.toPath()); + // 从response对象获取输出流 + out = response.getOutputStream(); + // 将输入流中的数据读取出来,写到输出流中 + for (int b = -1; (b = in.read()) != -1; ) { + out.write(b); + } + } catch (Exception e) { + log.error("获取文件流失败:", e.getMessage(), e); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + log.error(e.getMessage(), e); + } finally { + if (out != null) { + out.close(); + } + } + } + } + } }