Commit 49194c849ed38e83d6c457761ed4e0668207bceb

Authored by 房远帅
1 parent 3ed04b02

下载:按上传文件名下载

@@ -13,9 +13,13 @@ import com.lframework.starter.web.inner.service.SecurityUploadRecordService; @@ -13,9 +13,13 @@ import com.lframework.starter.web.inner.service.SecurityUploadRecordService;
13 import io.swagger.annotations.Api; 13 import io.swagger.annotations.Api;
14 import io.swagger.annotations.ApiImplicitParam; 14 import io.swagger.annotations.ApiImplicitParam;
15 import io.swagger.annotations.ApiOperation; 15 import io.swagger.annotations.ApiOperation;
16 -import java.io.File; 16 +import java.io.*;
  17 +import java.net.URLEncoder;
  18 +import java.nio.charset.StandardCharsets;
17 import javax.validation.constraints.NotBlank; 19 import javax.validation.constraints.NotBlank;
  20 +import javax.servlet.http.HttpServletResponse;
18 import lombok.extern.slf4j.Slf4j; 21 import lombok.extern.slf4j.Slf4j;
  22 +import org.apache.commons.lang3.StringUtils;
19 import org.springframework.beans.factory.annotation.Autowired; 23 import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.validation.annotation.Validated; 24 import org.springframework.validation.annotation.Validated;
21 import org.springframework.web.bind.annotation.GetMapping; 25 import org.springframework.web.bind.annotation.GetMapping;
@@ -43,7 +47,8 @@ public class SecurityDownloadController extends DefaultBaseController { @@ -43,7 +47,8 @@ public class SecurityDownloadController extends DefaultBaseController {
43 @ApiOperation("下载文件(获取签名URL)") 47 @ApiOperation("下载文件(获取签名URL)")
44 @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true) 48 @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true)
45 @GetMapping("/url") 49 @GetMapping("/url")
46 - public InvokeResult<String> getSecurityDownloadUrl(@NotBlank(message = "ID不能为空!") String id) { 50 + public InvokeResult<String> getSecurityDownloadUrl(@NotBlank(message = "ID不能为空!") String id,
  51 + String fileName) {
47 SecurityUploadRecord record = securityUploadRecordService.getById(id); 52 SecurityUploadRecord record = securityUploadRecordService.getById(id);
48 if (record == null) { 53 if (record == null) {
49 throw new AccessDeniedException(); 54 throw new AccessDeniedException();
@@ -53,14 +58,25 @@ public class SecurityDownloadController extends DefaultBaseController { @@ -53,14 +58,25 @@ public class SecurityDownloadController extends DefaultBaseController {
53 // throw new AccessDeniedException(); 58 // throw new AccessDeniedException();
54 // } 59 // }
55 60
56 - return InvokeResultBuilder.success(  
57 - UploadUtil.generatePresignedUrl(record.getUploadType().getCode(), record.getFilePath())); 61 + String url = UploadUtil.generatePresignedUrl(record.getUploadType().getCode(),
  62 + record.getFilePath());
  63 + if (StringUtils.isNotBlank(fileName)) {
  64 + try {
  65 + url = url + "&fileName=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name())
  66 + .replace("+", "%20");
  67 + } catch (UnsupportedEncodingException e) {
  68 + e.printStackTrace();
  69 + }
  70 + }
  71 +
  72 + return InvokeResultBuilder.success(url);
58 } 73 }
59 74
60 @ApiOperation("下载文件") 75 @ApiOperation("下载文件")
61 @ApiImplicitParam(value = "签名", name = "sign", paramType = "query", required = true) 76 @ApiImplicitParam(value = "签名", name = "sign", paramType = "query", required = true)
62 @GetMapping 77 @GetMapping
63 - public void download(@NotBlank(message = "签名不能为空!") String sign) { 78 + public void download(@NotBlank(message = "签名不能为空!") String sign, String fileName,
  79 + HttpServletResponse response) throws IOException {
64 Object val = redisHandler.hget("security-upload", sign); 80 Object val = redisHandler.hget("security-upload", sign);
65 log.info("val====" + val); 81 log.info("val====" + val);
66 if (val == null) { 82 if (val == null) {
@@ -73,6 +89,22 @@ public class SecurityDownloadController extends DefaultBaseController { @@ -73,6 +89,22 @@ public class SecurityDownloadController extends DefaultBaseController {
73 throw new AccessDeniedException(); 89 throw new AccessDeniedException();
74 } 90 }
75 91
76 - ResponseUtil.download(file); 92 + String downloadFileName = StringUtils.isBlank(fileName) ? file.getName() : fileName;
  93 + String encodedFileName = URLEncoder.encode(downloadFileName, StandardCharsets.UTF_8.name())
  94 + .replace("+", "%20");
  95 +
  96 + response.setContentType("application/octet-stream");
  97 + response.setCharacterEncoding(StandardCharsets.UTF_8.name());
  98 + response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
  99 + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
  100 +
  101 + try (InputStream in = new FileInputStream(file)) {
  102 + byte[] buffer = new byte[8192];
  103 + int len;
  104 + while ((len = in.read(buffer)) != -1) {
  105 + response.getOutputStream().write(buffer, 0, len);
  106 + }
  107 + response.getOutputStream().flush();
  108 + }
77 } 109 }
78 } 110 }