Showing
1 changed file
with
38 additions
and
6 deletions
| ... | ... | @@ -13,9 +13,13 @@ import com.lframework.starter.web.inner.service.SecurityUploadRecordService; |
| 13 | 13 | import io.swagger.annotations.Api; |
| 14 | 14 | import io.swagger.annotations.ApiImplicitParam; |
| 15 | 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 | 19 | import javax.validation.constraints.NotBlank; |
| 20 | +import javax.servlet.http.HttpServletResponse; | |
| 18 | 21 | import lombok.extern.slf4j.Slf4j; |
| 22 | +import org.apache.commons.lang3.StringUtils; | |
| 19 | 23 | import org.springframework.beans.factory.annotation.Autowired; |
| 20 | 24 | import org.springframework.validation.annotation.Validated; |
| 21 | 25 | import org.springframework.web.bind.annotation.GetMapping; |
| ... | ... | @@ -43,7 +47,8 @@ public class SecurityDownloadController extends DefaultBaseController { |
| 43 | 47 | @ApiOperation("下载文件(获取签名URL)") |
| 44 | 48 | @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true) |
| 45 | 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 | 52 | SecurityUploadRecord record = securityUploadRecordService.getById(id); |
| 48 | 53 | if (record == null) { |
| 49 | 54 | throw new AccessDeniedException(); |
| ... | ... | @@ -53,14 +58,25 @@ public class SecurityDownloadController extends DefaultBaseController { |
| 53 | 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 | 75 | @ApiOperation("下载文件") |
| 61 | 76 | @ApiImplicitParam(value = "签名", name = "sign", paramType = "query", required = true) |
| 62 | 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 | 80 | Object val = redisHandler.hget("security-upload", sign); |
| 65 | 81 | log.info("val====" + val); |
| 66 | 82 | if (val == null) { |
| ... | ... | @@ -73,6 +89,22 @@ public class SecurityDownloadController extends DefaultBaseController { |
| 73 | 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 | } | ... | ... |