上传文件
前台:
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 |
<%@
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>
|
配置:
1 2 3 4 5 6 |
<!--
多部分文件上传 -->
<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>
|
后台:
方式一:
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 |
@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());
InputStream
is=file.getInputStream();
int temp;
while ((temp=is.read())!=(- 1 ))
{
os.write(temp);
}
os.flush();
os.close();
is.close();
} catch (filenotfoundException
e) {
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 |
@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);
file.transferTo(newFile);
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 28 29 30 31 32 33 34 35 36 |
@RequestMapping ( "springUpload" )
public String
springUpload(HttpServletRequest request) throws IllegalStateException,
IOException
{
long startTime=System.currentTimeMillis();
CommonsMultipartResolver
multipartResolver= new CommonsMultipartResolver(
request.getsession().getServletcontext());
if (multipartResolver.isMultipart(request))
{
MultipartHttpServletRequest
multiRequest=(MultipartHttpServletRequest)request;
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自带的上传文件的方法要快的多!
相关阅读
java 文件上传demo
每次写代码都要查找,所以就放这里面备份了
基本pom引用:
<dependency>
<groupId>org.apache.httpcompone
大文件上传解决方案-up6-泽优软件
所有示例全部更新
示例:http://www.ncmem.com/webapp/up6/versions.aspx
控件:http://www.ncmem.com/webapp/up6/pack.aspx
说明:
破解qq群文件上传限制 日引流10000+(附:转化思路)
这两天“圈内”都流传着QQ群文件破群规无限制上传的漏洞。可以实现无限制的在任何群内上传任何文件,包括设置了禁止普通群成员上传