博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java常见3种文件上传速度对比和文件上传方法详细代码
阅读量:4315 次
发布时间:2019-06-06

本文共 7731 字,大约阅读时间需要 25 分钟。

在java里面文件上传的方式很多,最简单的依然是FileInputStream、FileOutputStream了,在这里我列举3种常见的文件上传方法代码,并比较他们的上传速度(由于代码是在本地测试,所以忽略网速的影响)

还是老规矩,大神请绕一下,里屋说话。

首先呢,使用springMVC原生上传文件方法,需要一些简单的配置,不多说,上图。

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

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
@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(
"Spring方法的运行时间:"
+String.valueOf(endTime-startTime)+
"ms"
);
        
return 
"/success"
;
    
}

  在这里故意加一个计时,待会就用它简单的比较上传时间问题(本人暂时还没能力处理资源占用问题,所以这里也不做比较)

2.第二位选手,采用file.Transto 来保存上传的文件,这是目前我认为最好的上传方式,也是我最喜欢的上传方式,代码简单,速度快。请看下面代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
     
* 采用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(
"采用file.Transto的运行时间:"
+String.valueOf(endTime-startTime)+
"ms"
);
        
return 
"/success"
;
    
}

  3.第三种采用流的方式上传,这种方法在新手学习的时候经常用到,但是我并不喜欢,因为它又慢又难写,请看

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
@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();
            
byte
[] bts = 
new 
byte
[
1024
];
            
//一个一个字节的读取并写入
            
while
(is.read(bts)!=-
1
)
            
{
                
os.write(bts);
            
}
           
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.分别选择同一个文件,稍微大一点(我这里上传的zookeeper3.3.6的安装包,大小为11M),以区别处他们的耗时差异(最好不实用ie,很容易崩溃,亲测)

  

  3.统计耗时,请看下图,结果一目了然。

 

 在此补充说明一点,如果你认为采用流的方式上传慢是因为我这里内存开辟小了,可以尝试开大一点,但是依然不影响他的速度最慢的地位,如果内存开的过大,反倒影响速度。

以上内容仅供学习,如果有需要源码的,请联系我。

 

解决使用Spring Boot、Multipartfile上传文件路径错误问题

彻底跟路径错误say拜拜!

题图:from Google

1.问题描述

  • 关键字: SpringMVC 4.2.4 、 Spring Boot 1.3.1 、Servlet 3.0 、文件上传
  • 报错信息: java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/IMG_20160129_132623.jpg (No such file or directory)
  • 问题源码: transferTo方法报错
    123456 7 8 9
    // 前端传入mulFileSource// 创建压缩前源文件File fileSourcePath = new File("tmp/source/"); File fileSource = new File(fileSourcePath, mulFileSource.getOriginalFilename()); if (!fileSourcePath.exists()) { fileSourcePath.mkdirs(); } // 将接收得图片暂存到临时文件中 mulFileSource.transferTo(fileSource);

2.问题分析

  • 首先,看源码中文件定义,相对路径,预期路径应该是项目路径/tmp/source/,但是报错确是一个系统临时文件路径(tomcat的)。
  • 其次,由于是transferTo方法报错,因此应该是该方法写入文件时报错,因此,我们跟入方法源码。
123456 7 8 9 10 11 12 13 14 15 16 17 18
public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest { //中间代码省略 /** * Spring MultipartFile adapter, wrapping a Servlet 3.0 Part object. */ @SuppressWarnings("serial") private static class StandardMultipartFile implements MultipartFile, Serializable { //中间代码省略 @Override public void transferTo(File dest) throws IOException, IllegalStateException { this.part.write(dest.getPath()); } } }
123456 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package org.apache.catalina.core;/** * Adaptor to allow {
@link FileItem} objects generated by the package renamed * commons-upload to be used by the Servlet 3.0 upload API that expects * { @link Part}s. */ public class ApplicationPart implements Part { //中间代码省略 @Override public void write(String fileName) throws IOException { File file = new File(fileName); if (!file.isAbsolute()) { file = new File(location, fileName); } try { fileItem.write(file); } catch (Exception e) { throw new IOException(e); } } }
  • 源码一目了然,使用Servlet3.0的支持的上传文件功能时,如果我们没有使用绝对路径的话,transferTo方法会在相对路径前添加一个location路径,即:file = new File(location, fileName);。当然,这也影响了SpringMVC的Multipartfile的使用。
  • 由于我们创建的File在项目路径/tmp/source/,而transferTo方法预期写入的文件路径为/tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/,我们并没有创建该目录,因此会抛出异常。

3.问题解决方案

  1. 使用绝对路径
  2. 修改location的值
    这个location可以理解为临时文件目录,我们可以通过配置location的值,使其指向我们的项目路径,这样就解决了我们遇到的问题。
    在Spring Boot下配置location,可以在main()方法所在文件中添加如下代码:
    123456 7 8 9
    /** * 文件上传临时路径 */ @Bean MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setLocation("/app/pttms/tmp"); return factory.createMultipartConfig(); }

     

     

     

     

 

表单,enctype 和 input 的type=file 即可,例子使用单文件上传

<form enctype="multipart/form-data" method="POST"

action="/file/fileUpload">
图片<input type="file" name="file" />
<input type="submit" value="上传" />
</form>
1
2
3
4
5
@Controller
@RequestMapping("/file")
public class UploadFileController {
@Value("${file.upload.path}")
private String path = "upload/";

@RequestMapping(value = "fileUpload", method = RequestMethod.POST)

@ResponseBody
public String fileUpload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "false";
}
String fileName = file.getOriginalFilename();
File dest = new File(path + "/" + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest); // 保存文件
return "true";
} catch (Exception e) {
e.printStackTrace();
return "false";
}
}
}
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
运行在保存文件 file.transferTo(dest) 报错
问题
dest 是相对路径,指向 upload/doc20170816162034_001.jpg
file.transferTo 方法调用时,判断如果是相对路径,则使用temp目录,为父目录
因此,实际保存位置为 C:\Users\xxxx\AppData\Local\Temp\tomcat.372873030384525225.8080\work\Tomcat\localhost\ROOT\upload\doc20170816162034_001.jpg

一则,位置不对,二则没有父目录存在,因此产生上述错误。

解决办法

transferTo 传入参数 定义为绝对路径

@Controller

@RequestMapping("/file")
public class UploadFileController {
@Value("${file.upload.path}")
private String path = "upload/";

@RequestMapping(value = "fileUpload", method = RequestMethod.POST)

@ResponseBody
public String fileUpload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "false";
}
String fileName = file.getOriginalFilename();
File dest = new File(new File(path).getAbsolutePath()+ "/" + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest); // 保存文件
return "true";
} catch (Exception e) {
e.printStackTrace();
return "false";
}
}
}
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
另外也可以 file.getBytes() 获得字节数组,OutputStream.write(byte[] bytes)自己写到输出流中。
参考

转载于:https://www.cnblogs.com/yuluoxingkong/p/10676933.html

你可能感兴趣的文章
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>
一款jQuery打造的具有多功能切换的幻灯片特效
查看>>
SNMP从入门到开发:进阶篇
查看>>
@ServletComponentScan ,@ComponentScan,@Configuration 解析
查看>>
unity3d 射弹基础案例代码分析
查看>>
thinksns 分页数据
查看>>
os模块
查看>>
LINQ to SQL vs. NHibernate
查看>>
基于Angular5和WebAPI的增删改查(一)
查看>>
windows 10 & Office 2016 安装
查看>>
最短路径(SP)问题相关算法与模板
查看>>
js算法之最常用的排序
查看>>
Python——交互式图形编程
查看>>
经典排序——希尔排序
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>