Commit 6c2d2058 by HlQ

[style] 代码优化

1 parent b45e2685
......@@ -11,12 +11,12 @@ import org.dromara.hutool.core.date.TimeUtil;
import org.dromara.hutool.core.io.file.FileNameUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.net.url.UrlEncoder;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.crypto.SecureUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
......@@ -30,7 +30,6 @@ import vion.vo.FileInfoVO;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
@RestController
......@@ -68,6 +67,9 @@ public class FileController {
@SaCheckPermission(value = "file:download", orRole = "admin")
public ResponseEntity<FileSystemResource> download(Long id) {
var fileInfo = fileService.getById(id);
if (ObjUtil.isNotNull(fileInfo)) {
return ResponseEntity.notFound().build();
}
var file = FileUtil.file(fileInfo.getUrl());
if (!FileUtil.exists(file)) {
......@@ -76,9 +78,8 @@ public class FileController {
FileSystemResource resource = new FileSystemResource(file);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + UrlEncoder.encodeAll(file.getName(),
StandardCharsets.UTF_8).replace("+", "%20"));
headers.setContentType(MediaType.valueOf(MediaType.APPLICATION_OCTET_STREAM_VALUE));
headers.setContentDisposition(ContentDisposition.attachment().filename(file.getName()).build());
return ResponseEntity.ok()
.headers(headers)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!