springMVC3学习(十一)--文件上传CommonsMultipartFile
2014-12-08 16:53
316 查看
使用springMVC提供的CommonsMultipartFile类进行读取文件
需要用到上传文件的两个jar包 commons-logging.jar、commons-io-xxx.jar
1、在spring配置文件中配置文件上传解析器
[html]
view plaincopyprint?
<!-- 文件上传解析器 -->
<bean
id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property
name="defaultEncoding"
value="utf-8"></property>
<property
name="maxUploadSize"
value="10485760000"></property><!-- 最大上传文件大小
-->
<property
name="maxInMemorySize"
value="10960"></property>
</bean>
2、文件上传页面(index.jsp)
[html]
view plaincopyprint?
<!-- method必须为post 及enctype属性-->
<form
action="fileUpload.do"
method="post"
enctype="multipart/form-data">
<input
type="file"
name="file">
<input
type="submit"
value="上传">
</form>
3、FileController类
[java]
view plaincopyprint?
@Controller
public class FileController{
@RequestMapping("/fileUpload.do")
public String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse
response){
long startTime=System.currentTimeMillis();
//获取开始时间
if(!file.isEmpty()){
try {
//定义输出流 将文件保存在D盘 file.getOriginalFilename()为获得文件的名字
FileOutputStream os = new FileOutputStream("D:/"+file.getOriginalFilename());
InputStream in = file.getInputStream();
int b = 0;
while((b=in.read())!=-1){
//读取文件
os.write(b);
}
os.flush(); //关闭流
in.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
long endTime=System.currentTimeMillis();
//获取结束时间
System.out.println("上传文件共使用时间:"+(endTime-startTime));
return "success";
}
}
上传了一个3.54M的PDF文件 共使用29132毫秒(以自己计算机实际为准)
上面计算了上传文件所使用时间,目的为了和下篇另一种上传方法进行比较 看哪个效率更高
测试URL: http://localhost:8080/spring/
需要用到上传文件的两个jar包 commons-logging.jar、commons-io-xxx.jar
1、在spring配置文件中配置文件上传解析器
[html]
view plaincopyprint?
<!-- 文件上传解析器 -->
<bean
id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property
name="defaultEncoding"
value="utf-8"></property>
<property
name="maxUploadSize"
value="10485760000"></property><!-- 最大上传文件大小
-->
<property
name="maxInMemorySize"
value="10960"></property>
</bean>
<!-- 文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="10485760000"></property><!-- 最大上传文件大小 --> <property name="maxInMemorySize" value="10960"></property> </bean>
2、文件上传页面(index.jsp)
[html]
view plaincopyprint?
<!-- method必须为post 及enctype属性-->
<form
action="fileUpload.do"
method="post"
enctype="multipart/form-data">
<input
type="file"
name="file">
<input
type="submit"
value="上传">
</form>
<!-- method必须为post 及enctype属性--> <form action="fileUpload.do" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="上传"> </form>
3、FileController类
[java]
view plaincopyprint?
@Controller
public class FileController{
@RequestMapping("/fileUpload.do")
public String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse
response){
long startTime=System.currentTimeMillis();
//获取开始时间
if(!file.isEmpty()){
try {
//定义输出流 将文件保存在D盘 file.getOriginalFilename()为获得文件的名字
FileOutputStream os = new FileOutputStream("D:/"+file.getOriginalFilename());
InputStream in = file.getInputStream();
int b = 0;
while((b=in.read())!=-1){
//读取文件
os.write(b);
}
os.flush(); //关闭流
in.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
long endTime=System.currentTimeMillis();
//获取结束时间
System.out.println("上传文件共使用时间:"+(endTime-startTime));
return "success";
}
}
@Controller public class FileController{ @RequestMapping("/fileUpload.do") public String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response){ long startTime=System.currentTimeMillis(); //获取开始时间 if(!file.isEmpty()){ try { //定义输出流 将文件保存在D盘 file.getOriginalFilename()为获得文件的名字 FileOutputStream os = new FileOutputStream("D:/"+file.getOriginalFilename()); InputStream in = file.getInputStream(); int b = 0; while((b=in.read())!=-1){ //读取文件 os.write(b); } os.flush(); //关闭流 in.close(); os.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } long endTime=System.currentTimeMillis(); //获取结束时间 System.out.println("上传文件共使用时间:"+(endTime-startTime)); return "success"; } }
上传了一个3.54M的PDF文件 共使用29132毫秒(以自己计算机实际为准)
上面计算了上传文件所使用时间,目的为了和下篇另一种上传方法进行比较 看哪个效率更高
测试URL: http://localhost:8080/spring/
相关文章推荐
- springMVC3学习(十一)--文件上传CommonsMultipartFile
- springMVC3学习(十一)--文件上传CommonsMultipartFile
- springMVC3学习(十一)--文件上传CommonsMultipartFile
- springMVC3学习(十一)--文件上传CommonsMultipartFile
- springMVC3学习(十一)--文件上传CommonsMultipartFile
- springMVC3学习(十一)--文件上传CommonsMultipartFile
- springMVC3学习(十一)--文件上传CommonsMultipartFile
- springMVC3学习(十一)--文件上传CommonsMultipartFile
- spring 注解上传文件 @RequestParam(value = "file1", required = false)
- spring mvc MultipartFile 上传文件错误解决
- springMVC3学习(十二)--文件上传优化CommonsMultipartResolver
- springmvc+ajaxFileUpload上传文件(前后台彻底分离的情况下)
- Spring——注解上传文件.@RequestParam(value = "file1", required = false) ;
- springmvc + ajaxfileupload 实现异步上传文件(图片)
- 用Spring实现文件上传(CommonsMultipartFile)!
- ajaxfileupload+springMVC上传文件
- SpringMVC + ajaxfileupload的多文件上传
- springMVC3学习(十二)--文件上传优化CommonsMultipartResolver
- ajaxfileupload.js+springMVC实现无刷新文件上传
- springMVC3学习(十二)--文件上传优化CommonsMultipartResolver