多部分表单 POST 提交获取损坏文件的请求

multipart form POST submit request fetching broken files

我无法正确上传文件。

我的问题总结: 任何正在上传的文件(*.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){
...
}
}

我的疑问:内容类型是否对这种行为负责?我在 web.xml 中强制 CharacterEncodingFilter<init-param> 值为 ISO-8859-15。我也在 jsp 页面中使用了 ISO-8859-15 编码,因为我也必须处理欧洲文本。 欢迎任何帮助或指导。提前致谢。

我认为问题不在于 acceptCharset="ISO-8859-15"
我整理了一个测试用例(使用 Spring boot ) based on a Getting Started guide from Spring IO website: 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>