I am having trouble uploading files correctly.
Summary of my problem: Any files being uploaded (*.docx, *.pdf, *.jpg/png/bmp etc.) are received as broken on server side.
My Environment: JSP + Spring 3 MVC + Java.
I have tried different approaches including one suggested by here by BalusC, but failed.
These are example uploads which have failed badly.
My Code:
tempform.jsp
<form:form method="POST" acceptCharset="ISO-8859-15" action="submit.htm"
commandName="commandform" enctype="multipart/form-data" >
...
<input name ="file0[] type="file" id="file0" multiple>
...
<input type="submit" name="submit">
controller.java
@RequestMapping(value = "/submit", method = RequestMethod.POST)
public ModelAndView submitRequest(@ModelAttribute("commandform") Request req, HttpServletRequest request, HttpServletResponse response, ModelMap model){
try {
MultipartHttpServletRequest tempPart = (MultipartHttpServletRequest) httpReq;
//file being transported is original.jpg and is only one.
MultipartFile filePart = tempPart.getFile("file0[]");
String fileName1 = filePart.getOriginalFilename();
InputStream fileContent = filePart.getInputStream();
//printing file here in this step for debugging purpose. Using jpg type only for example purpose.
BufferedImage bImageFromConvert = ImageIO.read(fileContent);
ImageIO.write(bImageFromConvert, "jpg", new File(
"e:/mynewfile.jpg"));
//file is created at location but with distorted version as shown in image.
...
}catch(Exception ex){
...
}
}
My doubt: Is content type responsible for this behavior? I am forcing CharacterEncodingFilter
with <init-param>
value as ISO-8859-15
in my web.xml
. I have used ISO-8859-15
encoding in jsp page as well because I have to deal with European text as well.
Any help or guidance will be most welcome. Thanks in advance.