0

I'm trying to implement file upload functionality in a Spring application.

Currently, I am using the multiple attribute of HTML5 forms to send multiple files to the server. The files are htting the controller, but I am having issues transferring them to the server destination.

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleUpload(@RequestParam("files[]") List<MultipartFile> files, Model model) {

    String fileName;
    File transferFile;
    String filePath = System.getProperty("catalina.base") + File.separator + "resources" + File.separator;
    ArrayList<File> fileList = new ArrayList<File>(files.size());
    ArrayList<String> fileNameList = new ArrayList<String>(files.size());


    for (MultipartFile file : files) {

        fileName = filePath + file.getOriginalFilename();
        transferFile = new File(fileName);
        fileNameList.add(fileName);

        try {
            if (transferFile.exists()) {
                logger.info("Successful Transfer!");
                file.transferTo(transferFile);
            else 
                logger.info("Could not create file at " + fileName);

I left out the catch blocks and other logging but the transferFile object is created but it does not exist at the location.

How can I create the file at the specified location?

4

1 回答 1

2
  1. 检查您在表单标签中添加了 enctype='multipart/form-data' 。

检查你在配置文件 spring.xml 中定义

 <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <!-- one of the properties available; the maximum file size in bytes -->
          <beans:property name="maxUploadSize" value="100000000" />
 </beans:bean>

2.检查文件标签的名称属性并在控制器方法handleUpload中使用相同的名称。

3.检查文件是否由您在从用户传输文件的指定位置创建,即检查 transferFile 在目的地创建文件与否。

于 2013-05-06T10:42:28.497 回答