[英]Spring Thymeleaf Required request part 'file' is not present
I'm trying to upload big files using Spring and CommonsMultipartResolver (something very similar to this related topic 我正在尝试使用Spring和CommonsMultipartResolver上传大文件(与该相关主题非常相似
Uploading even a small files (few Kb) results as: 上载一个很小的文件(几Kb)的结果是:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this
as a fallback.
Fri Mar 02 23:54:59 MSK 2018
There was an unexpected error (type=Bad Request, status=400).
Required request part 'file' is not present
Here is my UploadController.java "/upload" method code: 这是我的UploadController.java“ / upload”方法代码:
@PostMapping(value = "/upload", consumes =
MediaType.MULTIPART_FORM_DATA_VALUE)
public String singleFileUpload(@RequestParam("file")
MultipartFile file,
RedirectAttributes redirectAttributes){
if (file.isEmpty()){
redirectAttributes.addFlashAttribute("message", "Файл для загрузки не выбран. Выберите файл в диалоге");
return "redirect:/files";
}
try {
BufferedOutputStream outputStream = new BufferedOutputStream(
new FileOutputStream(
new File(UPLOAD_LOCATION + "\\",
file.getOriginalFilename())));
outputStream.write(file.getBytes());
outputStream.flush();
outputStream.close();
String.format("Файл %s успешно загружен", file.getOriginalFilename()));
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/files";
}
my form code: 我的表格代码:
<div class="form-group"> <form method="post" enctype="multipart/form-data" action="/upload"> <input type="file" class="file" name="file" /> <small id="fileHelp" class="form-text text-muted">Выберите файл для загрузки в файловый каталог приложения. После загрузки файл автоматически появится в этом списке. </small> <button type="submit" class="btn btn-default">Загрузить на сервер</button> </form> </div>
and the annotation bean definition: 和注解bean定义:
@Bean
public MultipartResolver multipartResolver() { CommonsMultipartResolver
multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(10 * 1024 * 1024 * 1024); // 10 GB
multipartResolver.setMaxUploadSizePerFile(3 * 1024 * 1024 * 1024); // 3 GB
return multipartResolver;
}
and application.properties: 和application.properties:
spring.http.multipart.max-file-size=3000MB
spring.http.multipart.max-request-size=3000MB
I have a correct attribute name "file" at form input and @RequestParam
value, also I wrote a MediaType at @PostMapping
annotation, but it still fails to work. 我在表单输入处具有正确的属性名称“文件”,并具有
@RequestParam
值,我还在@PostMapping
批注中编写了MediaType,但仍然无法正常工作。 Also, i've noticed, that when I'm not defining CommonsMultipartResolver bean, the upload works, but only with files less than 300 Mb... 另外,我注意到,当我未定义CommonsMultipartResolver bean时,上传有效,但仅适用于小于300 Mb的文件...
What do I do wrong? 我做错了什么?
尝试将以下属性添加到您的.properties文件中:
multipart.enabled=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.