Spring MVC文件上传

来源:互联网 发布:php 图片转base64 编辑:程序博客网 时间:2024/06/03 17:48

Spring MVC处理文件上传有两种方法:

  1. 通过Commons-fileupload来实现
  2. 利用Servlet3.0及其更高的版本的内置支持(Tomcat7才支持Servlet3)
    这里介绍第一种方法

步骤:

1 . 通过Commons-fileupload来实现
需要导入Commons-fileupload.jar和Commons-io.jar两个jar包,需要在Struts2的lib找
2 .spring mvc的配置文件配置解析器(可以查中文文档)

 <bean id="multipartResolver"    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    <!-- 支持的其中一个属性,支持的最大文件大小,以字节为单位 -->    <property name="maxUploadSize" value="10000000000"/>    <property name="defaultEncoding" value="utf-8"/>    <!-- 最大缓存 -->    <property name="maxInMemorySize" value="100000"/></bean>

3 .写fileuploadController类

@RequestMapping("/upload")//  方法参数中@RequestParam("file")必选加,不加就报错    public String fileupload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest req) throws IOException{//      获取客户端本地驱动器中的初始文件名字,即上传的文件名字        String fileName=file.getOriginalFilename();//      获取文件路径,这里是在工程根目录下找,需要先在WebRoot下新建                 fileupload文件夹        String path=req.getServletContext().getRealPath("/fileupload");        InputStream is=file.getInputStream();//      创建File对象        File file1= new File(path,fileName);//      创建一个向指定 File 对象表示的文件中写入数据的文件输出流。        OutputStream os=new FileOutputStream(file1);        int length=0;        byte[] buffer=new byte[400];//      从输入流读取一定字符放入到buffer数组中        while((length=is.read(buffer))!=-1)//          从buffer数组中读取字符写入输出流中            os.write(buffer,0,length);        os.close();        is.close();        return "/index.jsp";    }

4 .JSP中写提交表单
enctype 属性规定在发送到服务器之前应该如何对表单数据进行编码。
multipart/form-data表示不编码
必须包含<input type="file" name="file">会变成一个按钮选择文件

<!-- 为了上传文件,enctype必须设置为multipart/form-data -->  <form action="upload.do" method="post"  enctype="multipart/form-data">  select a file <input type="file" name="file"><br> <input type="submit" value="submit">  </form>


批量上传:
思路:其实步骤和上面单个上传差不多,批量上传就是利用数组来实现

<form action="batch.do" method="post"  enctype="multipart/form-data">  select a file <input type="file" name="file"><br>  select a file<input type="file" name="file"><br> <input type="submit" value="submit">  </form>
@RequestMapping("/batch")    public String fileupload(@RequestParam("file")CommonsMultipartFile file[],HttpServletRequest req) throws IOException{        for(int i=0;i<file.length;i++){        String fileName=file[i].getOriginalFilename();        String path=req.getServletContext().getRealPath("/fileupload");        InputStream is=file[i].getInputStream();        File file1= new File(path,fileName);        OutputStream os=new FileOutputStream(file1);        int length=0;        byte[] buffer=new byte[400];        while((length=is.read(buffer))!=-1)            os.write(buffer,0,length);        os.close();        is.close();        }        return "/index.jsp";    }

在HTML5之前,想批量上传,表单中必须写多个input,HTML5后,只需要

 <form action="batch.do" method="post"  enctype="multipart/form-data">  select a file <input type="file" name="file" multiple><br> <input type="submit" value="submit">  </form>

便可以一个按钮供选择一个或者多个文件,好处多多。
原来的方式,选文件的时候必须一个个点击上传(按钮的数量即最大上传文件数量和写的input的个数一样),不灵活,如果有一个按钮没选文件,尽管选上文件的也会上传成功,但是没选上文件的也会报空指针异常