修复上传临时文件清理和七牛区域配置

This commit is contained in:
2026-07-24 15:59:23 +08:00
parent b48213df67
commit 97821cf371
7 changed files with 73 additions and 35 deletions
@@ -27,6 +27,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
@RestController
@@ -58,14 +59,16 @@ public class CommentController {
uploadSecurityValidator.validate(file);
String originalFilename = normalizeOriginalFilename(file.getOriginalFilename());
String objectName = snowflakeIdGenerator.nextIdString() + resolveFileExtension(originalFilename);
ObjectStorageFileVO fileVO = objectStorageFileService.uploadManagedObjectFile(
originalFilename,
file.getInputStream(),
file.getSize(),
file.getContentType(),
objectName
);
return Result.success(fileVO);
try (InputStream inputStream = file.getInputStream()) {
ObjectStorageFileVO fileVO = objectStorageFileService.uploadManagedObjectFile(
originalFilename,
inputStream,
file.getSize(),
file.getContentType(),
objectName
);
return Result.success(fileVO);
}
} catch (Exception e) {
e.printStackTrace();
return Result.error("评价图片上传失败: " + e.getMessage());
@@ -29,6 +29,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
/**
@@ -137,14 +138,16 @@ public class ObjectStorageFileController {
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = snowflakeIdGenerator.nextIdString() + fileExtension;
ObjectStorageFileVO fileVO = objectStorageFileService.uploadManagedObjectFile(
originalFilename,
file.getInputStream(),
file.getSize(),
file.getContentType(),
objectName
);
return Result.success(fileVO);
try (InputStream inputStream = file.getInputStream()) {
ObjectStorageFileVO fileVO = objectStorageFileService.uploadManagedObjectFile(
originalFilename,
inputStream,
file.getSize(),
file.getContentType(),
objectName
);
return Result.success(fileVO);
}
} catch (Exception e) {
e.printStackTrace();
return Result.error("文件上传失败: " + e.getMessage());
@@ -56,6 +56,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;
@@ -814,17 +815,19 @@ public class UserController {
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
String objectName = "avatar/" + userId + "/" + snowflakeIdGenerator.nextIdString() + fileExtension;
ObjectStorageFileVO fileVO = objectStorageFileService.uploadManagedObjectFile(
originalFilename,
file.getInputStream(),
file.getSize(),
file.getContentType(),
objectName
);
String avatarUrl = fileVO.getPrimaryUrl();
userService.updateUserAvatar(userId, avatarUrl);
return Result.success(objectStorageUrlSupport.toAccessibleUrl(avatarUrl));
try (InputStream inputStream = file.getInputStream()) {
ObjectStorageFileVO fileVO = objectStorageFileService.uploadManagedObjectFile(
originalFilename,
inputStream,
file.getSize(),
file.getContentType(),
objectName
);
String avatarUrl = fileVO.getPrimaryUrl();
userService.updateUserAvatar(userId, avatarUrl);
return Result.success(objectStorageUrlSupport.toAccessibleUrl(avatarUrl));
}
} catch (Exception e) {
e.printStackTrace();
return Result.error("头像上传失败: " + e.getMessage());
@@ -909,14 +912,16 @@ public class UserController {
fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
}
String objectName = "profile/" + userId + "/" + snowflakeIdGenerator.nextIdString() + fileExtension;
ObjectStorageFileVO fileVO = objectStorageFileService.uploadManagedObjectFile(
originalFilename,
file.getInputStream(),
file.getSize(),
file.getContentType(),
objectName
);
return Result.success(objectStorageUrlSupport.toAccessibleUrl(fileVO.getPrimaryUrl()));
try (InputStream inputStream = file.getInputStream()) {
ObjectStorageFileVO fileVO = objectStorageFileService.uploadManagedObjectFile(
originalFilename,
inputStream,
file.getSize(),
file.getContentType(),
objectName
);
return Result.success(objectStorageUrlSupport.toAccessibleUrl(fileVO.getPrimaryUrl()));
}
} catch (Exception e) {
e.printStackTrace();
return Result.error("资料图片上传失败: " + e.getMessage());