Uploading multiple files using Spring MVC 3.0.2 after HiddenHttpMethodFilter has been enabled

帅比萌擦擦* 提交于 2019-11-29 02:19:19
Victor Vorobey

For upload multiple files in one request I used this code:

I have such jsp:

<p>Select files to upload. Press Add button to add more file inputs.</p>
<table>
    <tr>
        <td><input name="files" type="file" multiple="true"/></td>
    </tr>
    <tr>
        <td><input name="files" type="file" multiple="true"/></td>
    </tr>
</table>
<br/><input type="submit" value="Upload" />

File upload class bean:

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class FileUploadForm {

    private CommonsMultipartFile [] files;

    public CommonsMultipartFile[] getFiles() {
        return files;
    }

    public void setFiles( CommonsMultipartFile[] files ) {
        this.files = files;
    }
}

Controller:

@Controller
@RequestMapping("/upload")
public class FileUploadController {

    @RequestMapping(method = RequestMethod.GET)
    public String displayForm(ModelMap modelMap) {
        modelMap.addAttribute( new FileUploadForm() );
        return "uploadForm.jsp";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String save(FileUploadForm uploadForm) {
        CommonsMultipartFile[] files = uploadForm.getFiles();
        if(files != null && files.length != 0) {
            for(MultipartFile file : files) {
                System.out.println( file.getOriginalFilename() );
            }
        }
        return "success.jsp";
    }
}

This code allows to upload multiple files in one request, and be possible to get instance of CommonsMultipartFile for each file.

The issue as mentioned in the question was fixed as of Spring 3.0.4. As such, if you happened to use that version or higher (yes, it is 4.x.x now), you would not need to read this question/answer(s) anymore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!