`

springMVC上传文件

阅读更多

(1)、加入jar包:

jxl-2.6.jar 
commons-fileupload-1.2.jar
commons-io-1.3.1.jar
当然还有spring-web.jar等spring的依赖包
(2)、applicationContext.xml文件加入:

<bean id="multipartResolver" 	
         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
         <property name="maxUploadSize" value="104857600"/>  
         <property name="maxInMemorySize" value="4096"/>  
</bean> 

 (3)、编写上传jsp

<form id="importform" name="importform"  action="" method="post" enctype="multipart/form-data">
	<input type="file" id="fileName" name="fileName"/>
</form>

 注意: method="post" enctype="multipart/form-data" 必须要写,否则会抛异常

(4)、编写controller

response.setContentType("text/html");   //必须,否则会抛异常 
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multipartRequest.getFile("fileName");
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics