springMVC3学习(十一)--文件上传CommonsMultipartFile

本文详细介绍了如何在SpringMVC中利用CommonsMultipartFile类进行文件上传,包括配置文件上传解析器、上传页面实现以及处理上传过程中的关键步骤。通过设置最大上传文件大小、文件编码等参数,实现高效、稳定的文件上传功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用springMVC提供的CommonsMultipartFile类进行读取文件

需要用到上传文件的两个jar包 commons-logging.jar、commons-io-xxx.jar


1、在spring配置文件中配置文件上传解析器

  1. <!-- 文件上传解析器 --> 
  2. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
  3.     <property name="defaultEncoding" value="utf-8"></property> 
  4.     <property name="maxUploadSize" value="10485760000"></property><!-- 最大上传文件大小 --> 
  5.     <property name="maxInMemorySize" value="10960"></property> 
  6. </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)

  1. <!-- method必须为post 及enctype属性--> 
  2. <form action="fileUpload.do" method="post" enctype="multipart/form-data"> 
  3.     <input type="file" name="file"> 
  4.     <input type="submit" value="上传"> 
  5. </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类

  1. @Controller 
  2. public class FileController{ 
  3.      
  4.     @RequestMapping("/fileUpload.do"
  5.     public String fileUpload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request,HttpServletResponse response){ 
  6.         long startTime=System.currentTimeMillis();   //获取开始时间 
  7.         if(!file.isEmpty()){ 
  8.             try
  9.                 //定义输出流 将文件保存在D盘    file.getOriginalFilename()为获得文件的名字  
  10.                 FileOutputStream os = new FileOutputStream("D:/"+file.getOriginalFilename()); 
  11.                 InputStream in = file.getInputStream(); 
  12.                 int b = 0
  13.                 while((b=in.read())!=-1){ //读取文件  
  14.                     os.write(b); 
  15.                 } 
  16.                 os.flush(); //关闭流  
  17.                 in.close(); 
  18.                 os.close(); 
  19.                  
  20.             } catch (FileNotFoundException e) { 
  21.                 e.printStackTrace(); 
  22.             } catch (IOException e) { 
  23.                 e.printStackTrace(); 
  24.             } 
  25.         } 
  26.         long endTime=System.currentTimeMillis(); //获取结束时间 
  27.         System.out.println("上传文件共使用时间:"+(endTime-startTime)); 
  28.         return "success"
  29.     } 
@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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值