当前位置:   article > 正文

SpringMVC上传文件的三种方式_控制层用commonsmultipartfile接收上传文件

控制层用commonsmultipartfile接收上传文件

直接上代码吧,大伙一看便知

这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/commonsmultipartresolver.java.html

前台:
<%@ page language=“java” contentType=“text/html; charset=utf-8”
pageEncoding=“utf-8”%>

Insert title here

采用流的方式上传文件

采用multipart提供的file.transfer方法上传文件

使用spring mvc提供的类的方法上传文件

配置:

后台:

方式一:
/*
* 通过流的方式上传文件
* @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"; 
}
  • 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

方式二:
/*
* 采用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"; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

方式三:
/*
*采用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"; 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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

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

fileName:test.rar
方法一的运行时间:14712ms
fileName:test.rar
方法二的运行时间:5ms
方法三的运行时间:4ms

第二次:我用一个50M的文件
方式一进度很慢,估计得要个5分钟

方法二的运行时间:67ms
方法三的运行时间:80ms

原文地址:https://www.cnblogs.com/fjsnail/p/3491033.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/895628
推荐阅读
相关标签
  

闽ICP备14008679号