I'm trying to upload big files using Spring and CommonsMultipartResolver (something very similar to this related topic
Uploading even a small files (few Kb) results as:
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:
@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
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:
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.
Also, i've noticed, that when I'm not defining CommonsMultipartResolver bean, the upload works, but only with files less than 300 Mb...
What do I do wrong?