[英]multipart form POST submit request fetching broken files
我無法正確上傳文件。
我的問題摘要:正在上傳的任何文件(* .docx,* .pdf,* .jpg / png / bmp等)都在服務器端收到損壞。
我的環境: JSP + Spring 3 MVC + Java。
我嘗試了不同的方法,包括BalusC 在此提出的方法,但失敗了。
這些是失敗嚴重失敗的示例上傳。
我的代碼: 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){
...
}
}
我懷疑 :內容類型是否對此行為負責? 我在我的web.xml
強制使用帶有<init-param>
值的CharacterEncodingFilter
作為ISO-8859-15
。 我在jsp頁面中也使用了ISO-8859-15
編碼,因為我還必須處理歐洲文本。 我們非常歡迎任何幫助或指導。 提前致謝。
我認為問題不在於acceptCharset="ISO-8859-15"
我根據Spring IO網站的入門指南整理了一個測試用例(使用Spring boot spring-boot ): Spring IO文件上傳示例
我還為一個正在進行文件上傳的項目編寫了一個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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.