文件上传https

This commit is contained in:
chen-xin-zhi 2025-03-19 16:36:40 +08:00
parent 43a4d9bf30
commit c18bc21de8
3 changed files with 70 additions and 10 deletions

View File

@ -21,6 +21,7 @@ import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Arrays; import java.util.Arrays;
@RestController @RestController
@ -41,13 +42,13 @@ public class FileController {
/** /**
* Web端文件上传 * Web端文件上传obs桶
* @param multipartFile 文件对象 * @param multipartFile 文件对象
* @param uploadFileRequest 业务类型 * @param uploadFileRequest 业务类型
* @return PutObjectResult * @return PutObjectResult
*/ */
@PostMapping("/uploadFile") @PostMapping("/uploadFile")
@Operation(summary = "Web端文件上传", description = "参数文件对象multipartFile), 业务类型(biz)权限管理员方法名uploadFile") @Operation(summary = "Web端文件上传obs桶", description = "参数文件对象multipartFile), 业务类型(biz)权限管理员方法名uploadFile")
public BaseResponse<String> uploadFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) { public BaseResponse<String> uploadFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) {
// 校验用户是否登录 // 校验用户是否登录
User loginUser = userService.getLoginUser(request); User loginUser = userService.getLoginUser(request);
@ -67,11 +68,11 @@ public class FileController {
/** /**
* 文件下载接口 * 文件下载obs桶)
* @param objectKey 具体的文件名含存储路径 * @param objectKey 具体的文件名含存储路径
*/ */
@GetMapping("downloadFile") @GetMapping("downloadFile")
@Operation(summary = "文件下载", description = "参数:文件对象(具体的文件名(含存储路径))权限所有人方法名downloadFile") @Operation(summary = "文件下载obs桶)", description = "参数:文件对象(具体的文件名(含存储路径))权限所有人方法名downloadFile")
public void downloadFile(@RequestParam String objectKey, HttpServletResponse response) { public void downloadFile(@RequestParam String objectKey, HttpServletResponse response) {
iHweiYunOBSService.downloadFile(objectKey, response); iHweiYunOBSService.downloadFile(objectKey, response);
} }
@ -79,6 +80,62 @@ public class FileController {
// 优化设置一个合理的缓冲区大小
private static final int BUFFER_SIZE = 8192; // 8 KB
/**
* 文件下载服务器
* @param filename 文件名
* @param response 响应体
* @throws IOException
*/
@GetMapping("/download/{filename}")
@Operation(summary = "文件下载(服务器)", description = "参数文件名权限所有人方法名downloadFileFromServe")
public void downloadFileFromServe(@PathVariable String filename, HttpServletResponse response) throws IOException {
// 文件所在的路径
String filePath = "/www/Fonts/" + filename;
File file = new File(filePath);
// 检查文件是否存在
if (!file.exists()) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;
}
// 设置响应头告诉浏览器这是一个下载文件
response.setContentType("application/octet-stream"); // 通用的文件流类型
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
response.setContentLengthLong(file.length()); // 使用 setContentLengthLong 适应大文件
// 设置缓存相关的 HTTP
long lastModified = file.lastModified();
response.setDateHeader("Last-Modified", lastModified); // 文件最后修改时间
response.setHeader("Cache-Control", "public, max-age=86400"); // 缓存一天24小时
response.setHeader("ETag", String.valueOf(lastModified)); // 使用文件最后修改时间作为 ETag
// 检查浏览器缓存是否有效
String ifNoneMatch = response.getHeader("If-None-Match");
if (ifNoneMatch != null && ifNoneMatch.equals(String.valueOf(lastModified))) {
// 如果 ETag 匹配表示文件没有变化返回 304 Not Modified
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
// 使用 BufferedInputStream BufferedOutputStream 提高性能
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream(), BUFFER_SIZE)) {
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
// 从文件中读取数据并写入响应输出流
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush(); // 确保所有数据都已写入响应
}
}
@ -123,4 +180,6 @@ public class FileController {
String filename = uuid + "-" + multipartFile.getOriginalFilename(); String filename = uuid + "-" + multipartFile.getOriginalFilename();
return String.format("%s/%s", fileUploadBizEnum.getValue(), filename); return String.format("%s/%s", fileUploadBizEnum.getValue(), filename);
} }
} }

View File

@ -142,7 +142,7 @@ public class BannerController {
* 小程序用户根据类型查看轮播图 * 小程序用户根据类型查看轮播图
* @return 轮播图列表 * @return 轮播图列表
*/ */
@GetMapping("/query") @PostMapping("/query")
@Operation(summary = "小程序用户根据类型查看轮播图", description = "参数:无,权限:所有人,方法名:queryBannerByType") @Operation(summary = "小程序用户根据类型查看轮播图", description = "参数:无,权限:所有人,方法名:queryBannerByType")
public BaseResponse<List<BannerVO>> queryBannerByType(@RequestBody CommonStringRequest commonStringRequest) { public BaseResponse<List<BannerVO>> queryBannerByType(@RequestBody CommonStringRequest commonStringRequest) {
if (commonStringRequest == null || StringUtils.isBlank(commonStringRequest.getType())) { if (commonStringRequest == null || StringUtils.isBlank(commonStringRequest.getType())) {

View File

@ -75,11 +75,12 @@ springdoc:
server: server:
port: 9092 port: 8888
# ssl: ssl:
# key-store: classpath:carboner.cn.jks key-store: classpath:carboner.cn.jks
# key-store-password: 6gsn1hke4m4f7 key-store-password: 6gsn1hke4m4f7
# key-store-type: JKS key-store-type: JKS
servlet: servlet:
context-path: /api context-path: /api
# cookie 30 天过期 # cookie 30 天过期