码迷,mamicode.com
首页 > 编程语言 > 详细

java获取图片文件返回地址

时间:2015-06-17 11:25:04      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

private String uploadFile(HttpServletRequest req,String file_name){
        try {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
            // 获得文件:
            MultipartFile file = multipartRequest.getFile(file_name);
            String fileExtName = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1);
            if(StringUtils.isEmpty(fileExtName)){
                return "";
            }
            String realPath = req.getSession().getServletContext().getRealPath("");
            String fileName = DateTimeUtil.getDateTime2Str3S4() + "." + fileExtName;
            String imgPath = "upload"+File.separatorChar+"advImg" + File.separatorChar + fileName;
            String imgPathRealPath = realPath + File.separatorChar+ "upload"+File.separatorChar+"advImg" + File.separatorChar + fileName;
            File fi = new File(imgPathRealPath);

            if (!fi.exists()) {
                fi.mkdirs();
            }
            file.transferTo(fi);
            
            return imgPath;
        } catch (Exception e) {
            return "";
        }
    }

 

java获取图片文件返回地址

标签:

原文地址:http://www.cnblogs.com/liyangxj/p/4582480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!