I am getting a problem while implementing multipart file upload using spring boot 1.5.2.
Here is the situation, I have a mapping to handle file upload process.While I start the spring server, it starts without any error. The problem is that I would either able to upload the file perfectly fine or I would get null on all attribute in FileBucket object.
This situation would stay forever if I do not shutdown the server.
- If it could upload, it would upload fine for the rest of the time.
- If not, it won't work until I restart the server(likely more than one time)
Here is the mapping.
@RequestMapping(value = {"/api/upload"}, method = RequestMethod.POST)
public ResponseEntity<Map<String, Integer>> upload(@Valid FileBucket fileBucket, BindingResult result) throws IOException {
Session session = sessionFactory.openSession();
User user = (User) session.load(User.class, getUserId());
Map<String, Integer> model = new HashMap<String, Integer>();
if (result.hasErrors()) {
System.out.println("validation errors");
System.out.println(result);
session.close();
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
} else {
int documentId = saveDocument(fileBucket, user);
model.put("documentId", documentId);
session.close();
return new ResponseEntity<Map<String, Integer>>(model, HttpStatus.OK);
}
}
And the FileBucket
object
public class FileBucketConversation {
private MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
I have tried few ways to implement file upload, and still having the same situation.
Using
StandardServletMultipartResolver
.@Bean(name = "multipartResolver") public StandardServletMultipartResolver resolver() { return new StandardServletMultipartResolver(); }
Using
CommonsMultipartResolver
v1.3.2.@Bean(name="multipartResolver") public CommonsMultipartResolver multipartResolver () { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize(MAX_FILE_SIZE); return resolver; }
overriding
MultipartFilter
@Bean @Order(0) public MultipartFilter multipartFile() { MultipartFilter multipartFilter = new MultipartFilter(); multipartFilter.setMultipartResolverBeanName("multipartResolver"); return multipartFilter; }
Enable
spring.http.multipart
in properties filespring.http.multipart.enabled=true spring.http.multipart.max-file-size=20Mb spring.http.multipart.max-request-size=20Mb
I really have no clue where to start looking. The problem happen occasionally, it do not happen every time I start the server but most of the time. Hoping some one could help me.
Thank you.