我正在将spring-boot-starter-parent
从版本2.5.3
升级到3.0.0
我使用org.springframework.web.multipart.commons.CommonsMultipartResolver;
(扩展它)来满足我的文件上传需求
- 设置最大上传的大小。
- 正在获取文件参数
- 验证附件等
然而,在新版本中,CommonsMultipartResolver
不再受支持。我需要帮助来弄清楚我可以使用什么作为替代。
代码示例:
public class MyMultipartResolver extends CommonsMultipartResolver {
private boolean resolveLazily = false;
private String supportedMimeTypes;
@Autowired
private SessionService sessionService;
@Autowired
private UserFileUploadEventService userFileUploadEventService;
@Value("${file.supportedMimeTypes}")
public void setSupportedMimeTypes(String value) {
supportedMimeTypes = value;
}
@Value("${upload.document.max.file.size}")
private int maxFileSize;
@Value("${scep.executable.path}")
private String scepExePath;
@Value("${upload.document.directory}")
private String uploadedDocumentPath;
public TamsMultipartResolver() {
super();
}
@Override
public void setResolveLazily(boolean resolveLazily) {
this.resolveLazily = resolveLazily;
}
@Override
public MultipartHttpServletRequest resolveMultipart(final HttpServletRequest request) throws MultipartException {
Assert.notNull(request, "Request must not be null");
if (this.resolveLazily) {
return new DefaultMultipartHttpServletRequest(request) {
@Override
protected void initializeMultipart() {
MultipartParsingResult parsingResult = parseRequest(request);
validateAttachment(parsingResult.getMultipartFiles().getFirst("file"));
setMultipartFiles(parsingResult.getMultipartFiles());
setMultipartParameters(getSanitizedMultipartparameters(parsingResult));
}
};
} else {
MultipartParsingResult parsingResult = parseRequest(request);
validateAttachment(parsingResult.getMultipartFiles().getFirst("file"));
return new DefaultMultipartHttpServletRequest(request, parsingResult.getMultipartFiles(), getSanitizedMultipartparameters(parsingResult),
parsingResult.getMultipartParameterContentTypes());
}
}
private Map<String, String[]> getSanitizedMultipartparameters(MultipartParsingResult parsingResult) {
Map<String, String[]> mpParams = parsingResult.getMultipartParameters();
if (mpParams != null) {
HashMap<String, String[]> encodedParams = new HashMap<String, String[]>();
for (String key : mpParams.keySet()) {
encodedParams.put(key, AntiSamyHelper.sanitizeXSS(mpParams.get(key)));
}
return encodedParams;
}
return mpParams;
}
public void validateAttachment(MultipartFile file) throws BadRequestException {
final int fileSize = new Long(file.getSize()).intValue();
final String simpleFileName = file.getOriginalFilename();
if (simpleFileName.length() > 0) {
if (isFrequecyAttack()) {
throw new BadRequestException(RestConstants.FILE_UPLOAD_FREQUENCY_VIOLATION);
}
if (!isValidMimeType(file)) {
throw new BadRequestException(RestConstants.FILE_UPLOAD_INVALID_FILE_TYPE);
}
if (fileSize > maxFileSize) {
throw new BadRequestException(RestConstants.FILE_UPLOAD_INVALID_FILE_SIZE);
}
if (containsThreat(file)) {
throw new BadRequestException(RestConstants.FILE_UPLOAD_MALICIOUS_FILE_DETECTED);
}
} else {
throw new BadRequestException(RestConstants.FILE_UPLOAD_INVALID_FILE_TYPE);
}
}
1条答案
按热度按时间njthzxwz1#
我可以使用这里提到的库:github.com/spring-projects/spring-framework/issues/29562
下面是问题的等效代码:
public class MyMultipartResolver {