SpringMVC实现文件上传以及批量上传

1、所需要的jar:

<dependency>
 <groupId>commons-io</groupId>
 <artifactId>commons-io</artifactId>
  <version>2.4</version>
  </dependency>
        
<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId>commons-fileupload</artifactId>
  <version>1.3.1</version>
</dependency>

2、配置multipartResolver:

在Spring 的context 文件中配置此项:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="1048570000"></property>
    <property name="maxInMemorySize" value="40960"></property>
    <property name="defaultEncoding" value="utf-8"></property>
</bean>

注意:id 必须是multipartResolver 否则将无法正常上传文件,或抛出404 错误。

3、编写接收文件的Controller 

@RequestMapping("/uploadfile")
    public String uploadFile(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest request,ModelMap modelMap) throws IOException{
        InputStream fileInputSteam=file.getInputStream();
        System.err.println("请求了到了");
        String uploadFilePath=request.getRealPath("/fileupload");
        
        OutputStream outputStream=new FileOutputStream(new File(uploadFilePath, file.getOriginalFilename()));
        System.err.println(uploadFilePath);
        byte[] temp=new byte[400];
        int length=0;
        while ((length=fileInputSteam.read(temp))!=-1) {
            outputStream.write(temp, 0, length);
        }
        fileInputSteam.close();
        outputStream.flush();
        outputStream.close();
        return "uploadResult";
    }

说明:CommonsMultipartFile file 必须加上@RequestParam("file")的注解,其中"file" 是你在页面上上传文件的那个input 标签的name属性值。

4、构建页面:

<%@ page language="java"  import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>  
 <%@ page isELIgnored ="false" %>
<html>
<body>
<h2>up.jsp </h2>
<form action="uploadfile" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="file"><button  type="submit">上传</button>
</form>
</body>
</html>

说明:form的method 属性必须为POST enctype="multipart/form-data"

批量上传需要变更的地方只是将Controller中参数设置为文件数组即可,下面的代码相应的做循环处理。

如下:

@RequestMapping("/uploadfile")
    public String uploadFile(@RequestParam("file")CommonsMultipartFile[] files,HttpServletRequest request,ModelMap modelMap) throws IOException{
        
        for(CommonsMultipartFile file2:files){
            InputStream fileInputSteam=file2.getInputStream();
            System.err.println("请求了到了");
            String uploadFilePath=request.getRealPath("/fileupload");
            
            OutputStream outputStream=new FileOutputStream(new File(uploadFilePath, file2.getOriginalFilename()));
            System.err.println(uploadFilePath);
            byte[] temp=new byte[400];
            int length=0;
            while ((length=fileInputSteam.read(temp))!=-1) {
                outputStream.write(temp, 0, length);
            }
            fileInputSteam.close();
            outputStream.flush();
            outputStream.close();
            
        }
        
        
        return "uploadResult";
    }

 

posted @ 2016-10-03 00:34  sanemu  阅读(939)  评论(0编辑  收藏  举报