当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC上传文件的三种实现方式

SpringMVC上传文件的三种实现方式

2019年07月22日  | 移动技术网IT编程  | 我要评论

springmvc上传文件的三种实现方式,直接上代码吧,大伙一看便知

前台:

<%@ 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>insert title here</title>

</head>

<body>

<form name="serform" action="/springmvc006/fileupload" method="post" enctype="multipart/form-data">

<h1>采用流的方式上传文件</h1>

<input type="file" name="file">

<input type="submit" value="upload"/>

</form>

<form name="form2" action="/springmvc006/fileupload2" method="post" enctype="multipart/form-data">

<h1>采用multipart提供的file.transfer方法上传文件</h1>

<input type="file" name="file">

<input type="submit" value="upload"/>

</form>

<form name="form2" action="/springmvc006/springupload" method="post" enctype="multipart/form-data">

<h1>使用spring mvc提供的类的方法上传文件</h1>

<input type="file" name="file">

<input type="submit" value="upload"/>

</form>

</body>

</html> 

配置:

<!-- 多部分文件上传 -->

<bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver">

   <property name="maxuploadsize" value="104857600" />

   <property name="maxinmemorysize" value="4096" />

   <property name="defaultencoding" value="utf-8"></property>

</bean> 

后台:

方式一:

/*

   * 通过流的方式上传文件

   * @requestparam("file") 将name=file控件得到的文件封装成commonsmultipartfile 对象

   */

  @requestmapping("fileupload")

  public string fileupload(@requestparam("file") commonsmultipartfile file) throws ioexception {

     

     

    //用来检测程序运行时间

    long starttime=system.currenttimemillis();

    system.out.println("filename:"+file.getoriginalfilename());

     

    try {

      //获取输出流

      outputstream os=new fileoutputstream("e:/"+new date().gettime()+file.getoriginalfilename());

      //获取输入流 commonsmultipartfile 中可以直接得到文件的流

      inputstream is=file.getinputstream();

      int temp;

      //一个一个字节的读取并写入

      while((temp=is.read())!=(-1))

      {

        os.write(temp);

      }

      os.flush();

      os.close();

      is.close();

     

    } catch (filenotfoundexception e) {

      // todo auto-generated catch block

      e.printstacktrace();

    }

    long endtime=system.currenttimemillis();

    system.out.println("方法一的运行时间:"+string.valueof(endtime-starttime)+"ms");

    return "/success"; 

  } 

方式二:

/*

   * 采用file.transto 来保存上传的文件

   */

  @requestmapping("fileupload2")

  public string fileupload2(@requestparam("file") commonsmultipartfile file) throws ioexception {

     long starttime=system.currenttimemillis();

    system.out.println("filename:"+file.getoriginalfilename());

    string path="e:/"+new date().gettime()+file.getoriginalfilename();

     

    file newfile=new file(path);

    //通过commonsmultipartfile的方法直接写文件(注意这个时候)

    file.transferto(newfile);

    long endtime=system.currenttimemillis();

    system.out.println("方法二的运行时间:"+string.valueof(endtime-starttime)+"ms");

    return "/success"; 

  } 

方式三:

/*

   *采用spring提供的上传文件的方法

   */

  @requestmapping("springupload")

  public string springupload(httpservletrequest request) throws illegalstateexception, ioexception

  {

     long starttime=system.currenttimemillis();

     //将当前上下文初始化给 commonsmutipartresolver (多部分解析器)

    commonsmultipartresolver multipartresolver=new commonsmultipartresolver(

        request.getsession().getservletcontext());

    //检查form中是否有enctype="multipart/form-data"

    if(multipartresolver.ismultipart(request))

    {

      //将request变成多部分request

      multiparthttpservletrequest multirequest=(multiparthttpservletrequest)request;

      //获取multirequest 中所有的文件名

      iterator iter=multirequest.getfilenames();

       

      while(iter.hasnext())

      {

        //一次遍历所有文件

        multipartfile file=multirequest.getfile(iter.next().tostring());

        if(file!=null)

        {

          string path="e:/springupload"+file.getoriginalfilename();

          //上传

          file.transferto(new file(path));

        }

         

      }

      

    }

    long endtime=system.currenttimemillis();

    system.out.println("方法三的运行时间:"+string.valueof(endtime-starttime)+"ms");

  return "/success"; 

  } 

我们看看测试上传的时间:

第一次我用一个4m的文件:

filename:test.rar

方法一的运行时间:14712ms

filename:test.rar

方法二的运行时间:5ms

方法三的运行时间:4ms 

第二次:我用一个50m的文件

方式一进度很慢,估计得要个5分钟

方法二的运行时间:67ms

方法三的运行时间:80ms

从测试结果我们可以看到:用springmvc自带的上传文件的方法要快的多!

对于测试二的结果:可能是方法三得挨个搜索,所以要慢点。不过一般情况下我们是方法三,因为他能提供给我们更多的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网