Java SE 8 comes with the Servlet Spec 3.0 so I thought it would be very easy to process a multipart POST request, but I was wrong.
I always get zero parts, although I see in the Chrome network debugger that the payload contains two images!
What am I doing wrong?
Here is my Java Code processing the POST request:
if (request.isMultipartRequest()) {
Collection<Part> parts = request.getParts();
log.info("number of parts: "+parts.size());
for (Part part : parts) {
String fileName = getFileName(part);
log.info("fileName = "+fileName);
}
}
...
private String getFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
System.out.println("content-disposition header= "+contentDisp);
String[] tokens = contentDisp.split(";");
for (String token : tokens) {
if (token.trim().startsWith("filename")) {
return token.substring(token.indexOf("=") + 2, token.length()-1);
}
}
return "";
}
It always logs 0 parts.
I'm using Tomcat 7 and Java SE 8.
I also think it is very weird to have this getFileName()
method in my own code, I expected Java 8 to do that for me...
Any hints on how to make this work is very appreciated!