From 7ba1e8dea59261ba1beec68b989cfbfb6c87e099 Mon Sep 17 00:00:00 2001 From: wangxy <1356089412@qq.com> Date: Fri, 19 Apr 2024 09:46:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=8F=82=E6=95=B0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/FileRelationManager.java | 10 ++++++ .../reward/FileRelationController.java | 32 +++++++++++++++++++ 2 files changed, 42 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 d9d25d6..b600788 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 @@ -218,6 +218,16 @@ public class FileRelationManager { } + /** + * + * 文件列表啥 + * @return java.util.List + */ + public RewFileRelation getRewFileRelation(String fileId){ + return fileRelationService.getById(fileId); + } + + } 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 fccb5da..fe68dca 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 @@ -5,19 +5,23 @@ import com.hyp.common.config.HypConfig; import com.hyp.common.core.controller.BaseController; import com.hyp.common.core.domain.AjaxResult; import com.hyp.common.enums.BusinessType; +import com.hyp.common.exception.ServiceException; 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.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; @@ -25,6 +29,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.util.List; +import java.util.Objects; /** @@ -162,4 +167,31 @@ public class FileRelationController extends BaseController { } + /** + * 通用下载请求 + * + */ + @ApiOperation("文件下载") + @GetMapping("/download") + public void fileDownload(String fileId, HttpServletResponse response, HttpServletRequest request) { + try { + RewFileRelation rewFileRelation = fileRelationManager.getRewFileRelation(fileId); + if(Objects.isNull(rewFileRelation)){ + throw new ServiceException("下载文件不存在"); + } + String fileName = rewFileRelation.getFileName(); + if (!FileUtils.checkAllowDownload(fileName)) { + throw new Exception(com.hyp.common.utils.StringUtils.format("文件名称({})非法,不允许下载。 ", fileName)); + } + String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); + String filePath = HypConfig.getDownloadPath() + fileName; + response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); + FileUtils.setAttachmentResponseHeader(response, realFileName); + FileUtils.writeBytes(filePath, response.getOutputStream()); + } catch (Exception e) { + log.error("下载文件失败", e); + } + } + + }