MultipartResolver实现文件上传功能

时间:2022-01-05 21:59:46

springmvc默认的解析器里面是没有加入对文件上传的解析的,,使用springmvc对文件上传的解析器来处理文件上传的时需要用springmvc提供的multipartresolver的申明,又因为commonsmultipartresolver实现了multipartresolver接口,所以我们可以在springmvc配置文件中这样配置:

?
1
2
3
4
5
6
<bean id="multipartresolver"
   class="org.springframework.web.multipart.commons.commonsmultipartresolver">
   <property name="defaultencoding" value="utf-8" />
   <property name="maxuploadsize" value="10485760000" />
   <property name="maxinmemorysize" value="40960" />
 </bean>

 首先引入文件上传所需要的包,commons-logging-*.jar commons-io-*.jar  commons-fileupload-*.jar

新建一个jsp页面.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>文件上传</title>
</head>
<body>
  <%--<form action="user/fileupload" method="post" enctype="multipart/form-data">--%>
  <form action="user/fileupload" method="post" enctype="multipart/form-data">
    <input type="file" name="fileupload" />
    <input type="submit" value="上传" />
  </form>
</body>
</html>

springmvc上传文件的形式有很多,这里我介绍两种.

第一种,看controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package gd.hz.springmvc.controller;
 
import java.io.file;
import java.io.ioexception;
 
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.multipart.commons.commonsmultipartfile;
import org.springframework.web.servlet.modelandview;
 
@controller("usercontroller")
@requestmapping("user")
public class usercontroller {
 
  // 处理文件上传一
  @requestmapping(value = "fileupload", method = requestmethod.post)
  public modelandview fileupload(
      @requestparam("fileupload") commonsmultipartfile file) {
    // 获取文件类型
    system.out.println(file.getcontenttype());
    // 获取文件大小
    system.out.println(file.getsize());
    // 获取文件名称
    system.out.println(file.getoriginalfilename());
 
    // 判断文件是否存在
    if (!file.isempty()) {
      string path = "d:/" + file.getoriginalfilename();
      file localfile = new file(path);
      try {
        file.transferto(localfile);
      } catch (illegalstateexception e) {
        e.printstacktrace();
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
    return new modelandview("datasuccess");
  }
}

类commonsmultipartfile为我们提供了许多对文件处理的方法.例如文件大小,上传文件名称,文件类型,具体用法可以查看spring的文档.transferto就是将文件输出到指定地方. 

文件上传的第二种方法,这种方法比较常用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gd.hz.springmvc.controller;
 
import java.io.file;
import java.io.ioexception;
import java.util.iterator;
 
import javax.servlet.http.httpservletrequest;
 
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.multipart.multipartfile;
import org.springframework.web.multipart.multiparthttpservletrequest;
import org.springframework.web.multipart.commons.commonsmultipartresolver;
 
@controller("usercontroller")
@requestmapping("user")
public class usercontroller {
 
  // 处理文件上传二
  @requestmapping(value = "fileupload2", method = requestmethod.post)
  public string fileupload2(httpservletrequest request)
      throws illegalstateexception, ioexception {
    // 设置上下方文
    commonsmultipartresolver multipartresolver = new commonsmultipartresolver(
        request.getsession().getservletcontext());
 
    // 检查form是否有enctype="multipart/form-data"
    if (multipartresolver.ismultipart(request)) {
      multiparthttpservletrequest multirequest = (multiparthttpservletrequest) request;
 
      iterator<string> iter = multirequest.getfilenames();
      while (iter.hasnext()) {
 
        // 由commonsmultipartfile继承而来,拥有上面的方法.
        multipartfile file = multirequest.getfile(iter.next());
        if (file != null) {
          string filename = "demoupload" + file.getoriginalfilename();
          string path = "d:/" + filename;
 
          file localfile = new file(path);
          file.transferto(localfile);
        }
 
      }
    }
    return "datasuccess";
  }
}

 multiparthttpservletrequest提供了更加灵活的方法,可以获取多个文件和文件名,可以遍历获得每个文件.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://mylfd.iteye.com/blog/1893648