ssm框架下fileupload图片上传实践

1、加入两个jar包,commons-fileupload-1.3.jar、commons-io-1.2.jar


2、在配置文件applicationContext.xml加上以下配置

[html]  view plain  copy
  1. <!-- 定义文件解释器(文件上传) -->    
  2. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">      
  3.     <!-- 设置默认编码 -->    
  4.     <property name="defaultEncoding" value="utf-8"></property>    
  5.     <!-- 上传图片最大大小1M-->     
  6.     <property name="maxUploadSize" value="1048576"></property>      
  7. </bean>  

3、Controller层写java代码

[java]  view plain  copy
  1. import java.io.File;  
  2. import java.io.IOException;  
  3.   
  4. import javax.servlet.http.HttpServletRequest;  
  5.   
  6. import org.springframework.stereotype.Controller;  
  7. import org.springframework.web.bind.annotation.RequestMapping;  
  8. import org.springframework.web.bind.annotation.ResponseBody;  
  9. import org.springframework.web.multipart.MultipartFile;  
  10.   
  11.   
  12. @Controller  
  13. @RequestMapping("/file")  
  14. public class FileController {  
  15.       
  16.       
  17.     @RequestMapping("/test.do")  
  18.     @ResponseBody  
  19.     public String test(MultipartFile file,HttpServletRequest request) throws IOException{  
  20.         System.out.println("comming!");  
  21.         String path = request.getSession().getServletContext().getRealPath("/images");  
  22.         System.out.println("path>>"+path);  
  23.   
  24.         String fileName = file.getOriginalFilename();  
  25.         System.out.println("fileName>>"+fileName);  
  26.           
  27.         File dir = new File(path, fileName);  
  28.         System.out.println("dir.exists()>>"+dir.exists());  
  29.         if(!dir.exists()){  
  30.             dir.mkdirs();  
  31.         }  
  32.         System.out.println("dir.exists()>>"+dir.exists());  
  33. //      MultipartFile自带的解析方法  
  34.         file.transferTo(dir);  
  35.           
  36.         return "ok";  
  37.     }  
  38. }  

5、html和js

[html]  view plain  copy
  1. <form id="test">    
  2.     选择文件:<input data-role="none" type="file" name="file" width="120px">    
  3.     <button data-role="none" onclick="testUpload();">测试</button>  
  4. </form>  
[javascript]  view plain  copy
  1. function testUpload(){  
  2.     var form = new FormData(document.getElementById("test"));  
  3.       
  4.     $.ajax({  
  5.         url : url,  
  6.         data : form,  
  7.         type : 'post',  
  8.         processData:false,  
  9.         contentType:false,  
  10.         success : function(data){  
  11.             alert("成功")  
  12.         },  
  13.         error : function(data){  
  14.               
  15.         }  
  16.     });  
  17. }  

这样就大功告成,上传的图片在tomcat目录的

\webapps\你的项目名\images

里面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值