Spring3.0 文件上传与下载显示

由于需要设置表单的编码,就需要加上enctype="multipart/form-data"这段,这段的意思是设置表单的MIME编码,为文件流形式,设置之后,form里面的input值将

以2进制的方式传过去,那么在控制其中的request就不能得到值了,解决方式是在spring的配置文件中加入
<bean id="multipartResolver"          
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
加入这段,到这里还没有结束,如果按照传统的HttpServletRequest的方式,我们依然自动注入对象,必须将HttpServletRequest换为MultipartHttpServletRequest的方式,才能

即完成注入对象,又能得到文件流
具体代码如下
jsp


view plain copy to clipboard print ?
  1. < form  act ="...."  enctype ="multipart/form-data" >   
  2. < input  type ="text"   name ="name" />   
  3. < input  type ="file"   name ="file" />   
  4. < input  type ="submit"   value ="提交" />   
  5. </ form >   




java代码

view plain copy to clipboard print ?
  1. public  String register(MultipartHttpServletRequest request,User user){  
  2.     MultipartFile file  = request.getFile("file" );//注意,这里我们以这种方式获得文件对象,file为input的name属性   
  3.     //其他保存操作   
  4.      
  5. }  


这样我们就能即完成注入,又能活的文件对象

你可能感兴趣的:(Spring3.0 文件上传与下载显示)