SpringMVC 单文件和多文件上传

关于springmvc上传文件的具体介绍可参看spring的官方文档 The Web模块
这里只总结具体的简单代码实现。

1.springMVC单文件上传

1.1 解析器配置
1.1.1 使用CommonsMultipartResolver

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum/maxmemery file size in bytes -->
    <property name="maxUploadSize" value="12345678900"/>
        <property name="maxInMemorySize" value="20480"/>
        <property name="defaultEncoding" value="UTF-8"/>
</bean>

CommonsMultipartResolver, 需要使用commons-fileupload.jar包

1.1.2 使用CosMultipartResolver

<bean id="multipartResolver" class="org.springframework.web.multipart.cos.CosMultipartResolver">

    <!-- one of the properties available; the maximum/maxmemery file size in bytes -->
    <property name="maxUploadSize" value="12345678900"/>
        <property name="maxInMemorySize" value="20480"/>
        <property name="defaultEncoding" value="UTF-8"/>
</bean>

CosMultipartResolver, 需要使用cos.jar包

1.2 表单配置

<html>
    <head>
        <title>Upload a file please</title>
    </head>
    <body>
        <h1>Please upload a file</h1>
        <form method="post" action="upload.form" enctype="multipart/form-data">
            <input type="file" name="file"/>
            <input type="submit"/>
        </form>
    </body>
</html>

在上面这个表单里有一个input元素,这个元素的名字(“file”)和服务器端处理这个表单的bean中类型为byte[]的属性名相同。 在这个表单里我们也声明了编码参数(enctype=”multipart/form-data”)以便让浏览器知道如何对这个文件上传表单进行编码

1.3 定义 controller类实现上传的具体逻辑

@Controller
public class UploadController {
    @RequestMapping("/fileupload")
    public String upload(@RequestParam("filename") CommonsMultipartFile file , HttpServletRequest req) throws IOException {
        //解析路径
        String path = req.getRealPath("/fileupload");
        //解析文件名
        String filename = file.getOriginalFilename();
        //新建file对象
        File targetFile = new File(path,filename);
        //判断文件是否存在
        if (!targetFile.exists()){
            targetFile.mkdirs();//不存在新建
        }
        file.transferTo(targetFile);
        //成功后返回的页面
        return "index.jsp";
    }

2 多文件上传
解析器相同不需要配置

2.1 配置表单

<form action="uploadfiles" method="post" enctype="multipart/form-data">
    <input type="file" name="filename"/><br>
    <input type="file" name="filename"/><br>
    <input type="file" name="filename"/><br>
    <input type="submit" value="确定"/>
</form>

2.2 定义controller类

@RequestMapping("/uploadfiles")
     public String uploadFiles(@RequestParam("filename")CommonsMultipartFile[] files ,HttpServletRequest req) throws IOException {
        String path = req.getRealPath("/fileupload");
        for (int i = 0; i < files.length; i++) {
            InputStream is = files[i].getInputStream();
            OutputStream os = new FileOutputStream(new File(path,files[i].getOriginalFilename()));
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = is.read(buffer))!=-1){
                os.write(buffer,0,len);
            }
            os.close();
            is.close();
        }

        return "index.jsp";
    }
上一篇:如何给sublime text3安装汉化包?so easy 哦


下一篇:SpringMVC controller的几种形态