jsp

            <form action="<%=path%>/Indexmanage/fileUpload" method="post" enctype="multipart/form-data">
                <!-- type="file"这一标签必须同时添加name属性,否则在处理上传时,无法检测到上传文件 -->
                <input type="file" name="myfiles"/>
                <input type="submit" value="上传"/>                 
            </form>

controller

    /**
     * 文件上传
     */
    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public int fileUpload(@RequestParam MultipartFile[] myfiles, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if(myfiles.length <= 0){
            return 0;
        }
        MultipartFile myfile = myfiles[0];
        String fileName = myfile.getOriginalFilename();
        System.out.println("文件长度: " + myfile.getSize());
        System.out.println("文件类型: " + myfile.getContentType());
        System.out.println("文件名称: " + myfile.getName());
        System.out.println("文件原名: " + fileName);
        System.out.println("========================================");
        //如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
        String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
        File f = new File(realPath, fileName);
        //这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
        FileUtils.copyInputStreamToFile(myfile.getInputStream(), f);
    }

需要导入的包

import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.FileUtils;

maven依赖

    <dependency>
        <groupId>org.springframework</groupId>
         <artifactId>spring-web</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency> 
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>