我无法正确上传文件。
我的问题摘要:正在上传的任何文件(* .docx,* .pdf,* .jpg / png / bmp等)在服务器端收到损坏。
我的环境: JSP + Spring 3 MVC + Java。
我尝试了不同的方法,包括BalusC here提出的方法,但失败了。
这些是失败严重失败的示例上传。
我的代码:
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){
...
}
}
我怀疑:内容类型是否对此行为负责?我在CharacterEncodingFilter
中强制<init-param>
ISO-8859-15
值web.xml
为ISO-8859-15
。我在jsp页面中也使用了{{1}}编码,因为我还必须处理欧洲文本。
我们非常欢迎任何帮助或指导。提前谢谢。
答案 0 :(得分:2)
我不认为问题出在acceptCharset="ISO-8859-15"
上
我根据Spring IO网站的入门指南整理了一个测试用例(使用Spring boot spring-boot):Spring IO file upload example
我还为一个正在进行文件上传的项目编写了一个Spring 3 MVC控制器。它类似于我在下面显示的例子。
使用这个Spring启动测试用例,我可以使用UTF-8和ISO-8859-15上传您的示例图像。它工作正常。当然,我没有像你一样使用CharacterEncodingFilter
以下是我的一些代码,因此您可以与您的代码进行比较。
我希望它有所帮助。
Application.java:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
return factory.createMultipartConfig();
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
FileUploadController.java:
@Controller
public class FileUploadController {
@RequestMapping("/")
public String welcome() {
return "welcome";
}
@RequestMapping(value="/upload", method=RequestMethod.GET)
public @ResponseBody String provideUploadInfo() {
return "You can upload a file by posting to this same URL.";
}
@RequestMapping(value="/upload", method=RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + " into " + name;
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
}
welcome.jsp片段:
<%--
<form:form action="upload" method="POST" acceptCharset="UTF-8" enctype="multipart/form-data" >
--%>
<form:form action="upload" method="POST" acceptCharset="ISO-8859-15" enctype="multipart/form-data" >
<table>
<tr>
<td>
<!-- <input type="hidden" name="action" value="upload" /> -->
<strong>Please select a file to upload :</strong> <input type="file" name="file" />
</td>
</tr>
<tr>
<td>Name: <input type="text" name="name"><br />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Upload"> Press here to upload the file!
</td>
</tr>
</table>
</form:form>