[英][org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'image' is not present]
[英]Spring org.springframework.web.multipart.support.MissingServletRequestPartException, Required request part 'file' is not present
我試圖通過在 FormBodyPart 中使用它來將文件發送到控制器,而不是直接將文件發送給它。 這是制作文件集合的代碼
private void addFile(Collection<FormBodyPart> parts, File inputFile, String fileType)
throws ClassificationException {
if (inputFile == null) {
throw new ClassificationException("Null input file provided");
}
if (!inputFile.exists()) {
throw new ClassificationException("Input file not found: " + inputFile.getAbsolutePath());
}
if (fileType != null) {
String charset = "UTF-8";
parts.add(new FormBodyPart("file", new FileBody(inputFile, fileType, charset)));
} else {
parts.add(new FormBodyPart("file", new FileBody(inputFile, inputFile.getName())));
}
}
零件集合是一個數組列表,其中將包含文件。
這是我設置 Http 實體的代碼
HttpPost httppost = new HttpPost("http://localhost:9000/upload1");
MultipartEntity reqEntity1 = new MultipartEntity();
FormBodyPart part1;
for (Iterator i$ = parts.iterator(); i$.hasNext(); reqEntity1.addPart(part1)) {
part1 = (FormBodyPart) i$.next();
System.out.println(part1.getHeader());
}
httppost.setEntity(reqEntity1);
HttpResponse response = httpclient.execute(httppost);
System.out.println(response);
我的控制器方法聲明是
String index(@RequestParam("file") MultipartFile uploadfile)
我收到來自服務器的錯誤說明
[400] {"timestamp":1474898550131,"status":400,"error":"Bad Request","exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"必需的請求部分“文件”不存在","path":"/upload1"}
我的 dispatcher.xml 已經包含了 multipartResolver 的 bean。
我對網絡服務相當陌生,可能會犯一些愚蠢的錯誤。 請幫助我,提前致謝
驗證您是否有以下物品:
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipart = new CommonsMultipartResolver();
multipart.setMaxUploadSize(3 * 1024 * 1024);
return multipart;
}
@Bean
@Order(0)
public MultipartFilter multipartFilter() {
MultipartFilter multipartFilter = new MultipartFilter();
multipartFilter.setMultipartResolverBeanName("multipartResolver");
return multipartFilter;
}
並在applications.properties
:
# MULTIPART (MultipartProperties)
spring.http.multipart.enabled=true
# Enable support of multi-part uploads.
# spring.http.multipart.file-size-threshold=3 # Threshold after which files will be written to disk. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.location= /
# Intermediate location of uploaded files.
spring.http.multipart.max-file-size=10MB
# Max file size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.max-request-size=10MB
# Max request size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.resolve-lazily=false
# Whether to resolve the multipart request lazily at the time of file or parameter access.
spring.io Uploading Files 中有一個很好的例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.