首页 > 编程语言 > 详细

spring 上传图片

时间:2015-11-07 17:35:40      阅读:281      评论:0      收藏:0      [点我收藏+]
 1 @RequestMapping(value = "/upload",method = RequestMethod.POST)
 2     public String upload(@RequestParam(value = "file", required = false) MultipartFile file){
 3         // 判断文件是否为空
 4         if (!file.isEmpty()) {
 5             // 文件保存路径
 6             String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"+ file.getOriginalFilename();
 7            
 8             UUID uuid = UUID.randomUUID();
 9            
10             String filename = uuid + file.getOriginalFilename();
11             File targetFile = new File(filePath,filename);
12             if (!targetFile.exists()) {
13 
14                 try {
15                     targetFile.createNewFile();
16                 } catch (IOException e) {
17                     e.printStackTrace();
18                 }
19             }
20             // 转存文件
21             try {
22                 file.transferTo(targetFile);
23             } catch (IOException e) {
24                 e.printStackTrace();
25             }
26            
27         }
28         return "upload";
29     }

 

spring 上传图片

原文:http://www.cnblogs.com/leov/p/4945568.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!