现在的位置: 首页 > 综合 > 正文

spring mvc 图片上传

2014年02月08日 ⁄ 综合 ⁄ 共 5549字 ⁄ 字号 评论关闭
  1. /**  
  2.      * 保存个人风采  
  3.      *   
  4.      * @throws IOException  
  5.      *   
  6.      */  
  7.     @RequestMapping(value = "/saveImgs1")   
  8.     public String saveImgs(ModelMap modelMap) {   
  9.         /* 权限验证 */  
  10.         int uid = this.user.get().getUid();   
  11.         if (this.user.get().getUid() <= 0) {   
  12.             return "redirect:/error/index?code=auth.unlogin";   
  13.         }   
  14.         // 允许上传的文件格式的列表JPG、JPEG、GIF、BMP
      
  15.         final String[] allowtype = new String[] { ".JPG"".JPEG"".GIF",
      
  16.                 ".BMP"".PNG"".jpg"".jpeg"".gif"".bmp"".png" };
      
  17.         try {   
  18.             String realPath = CMConfig.getProperty("newstempurl"); // 文件上传的绝对路径
      
  19.   
  20.             File fileDir = new File(realPath);   
  21.             if (!fileDir.exists()) {   
  22.                 fileDir.mkdirs();   
  23.             }   
  24.             request.get().setCharacterEncoding("UTF-8");   
  25.             MultipartRequest mr = new MultipartRequest(request.get(), realPath,   
  26.                     4194304"UTF-8");   
  27.             // mr.get   
  28.             String filename = "";   
  29.             String description = "";   
  30.             Enumeration filesname = mr.getFileNames();   
  31.             Enumeration filesdesc = mr.getParameterNames();   
  32.             String newImgPath = ""// 新图片路径
      
  33.             String url = ""// 保存图片的url
      
  34.   
  35.             while (filesname.hasMoreElements()) {   
  36.                 String name = (String) filesname.nextElement();   
  37.                 String dc = (String) filesdesc.nextElement();   
  38.                 filename = mr.getFilesystemName(name);   
  39.                 description = mr.getParameter(dc);   
  40.                 // filedName = (String) filesname.nextElement();// 文件文本框的名称
      
  41.                 File uploadFile = mr.getFile(name);   
  42.                 if (null != uploadFile && uploadFile.length() > 0) {   
  43.                     String imgPath = uploadFile.getName();   
  44.                     // imgPath为原文件名
      
  45.                     int idx = imgPath.lastIndexOf(".");   
  46.                     // 文件后缀   
  47.                     String extention = imgPath.substring(idx);   
  48.                     java.util.Date dt = new java.util.Date(   
  49.                             System.currentTimeMillis());   
  50.                     SimpleDateFormat fmt = new SimpleDateFormat(   
  51.                             "yyyyMMddHHmmss");   
  52.                     Long now = System.currentTimeMillis();   
  53.                     String time = fmt.format(dt) + String.valueOf(now);   
  54.                     // 新的文件名(日期+后缀)
      
  55.                     newImgPath = this.user.get().getUid() + "_" + time   
  56.                             + extention;   
  57.                     int allowFlag = 0;   
  58.                     int allowedExtCount = allowtype.length;   
  59.                     for (; allowFlag < allowedExtCount; allowFlag++) {   
  60.                         if (allowtype[allowFlag].equals(extention))   
  61.                             break;   
  62.                     }   
  63.                     if (allowFlag == allowedExtCount) {   
  64.                         String message = "";   
  65.                         for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++) {   
  66.                             message += "*" + allowtype[allowFlag] + " ";   
  67.                         }   
  68.                         logger.debug(extention + "message:'请上传以下类型的文件"  
  69.                                 + message + "'");   
  70.                         return "redirect:/account/personal_style?exception=imgtoolong";   
  71.                     }   
  72.                     File f = new File(fileDir + "/" + newImgPath);   
  73.                     uploadFile.renameTo(f);   
  74.                     Date d = new Date(now);   
  75.                     String year = DateUtil.dateToString(d, "yyyy");   
  76.                     String month = DateUtil.dateToString(d, "MM");   
  77.                     String day = DateUtil.dateToString(d, "dd");   
  78.                     url = CMConfig.getProperty("image.upload.url") + "/" + year   
  79.                             + "/" + month + "/" + day + "/" + newImgPath;   
  80.                     FileMove.move(CMConfig.getProperty("newstempurl")   
  81.                             + newImgPath, CMConfig.getProperty("newsurl"),   
  82.                             newImgPath);   
  83.                 }   
  84.             }   
  85.             UDBUserDao userDao = AppContext   
  86.                     .getBean("userDao", UDBUserDao.class);   
  87.             if (url.trim().length() == 0) {   
  88.                 return "redirect:/account/personal_style?exception=imgnotnull";   
  89.             } else {   
  90.                 if (userDao.initSelfStyle(uid, url) > 0) {   
  91.                     // 增加积分   
  92.                     ScoreService scoreService = AppContext.getBean(   
  93.                             "scoreService", ScoreService.class);   
  94.                     scoreService.addScore(this.user.get().getUid(),   
  95.                             ScoreRuleType.ADD_STYLE, 0);   
  96.                     return "redirect:/account/personal_style?exception=success";   
  97.                 } else {   
  98.                     return "redirect:/account/personal_style?exception=error";   
  99.                 }   
  100.             }   
  101.   
  102.         } catch (UnsupportedEncodingException e) {   
  103.             // TODO Auto-generated catch block
      
  104.             e.printStackTrace();   
  105.         } catch (IOException e) {   
  106.             logger.debug("**************************************************");   
  107.             logger.debug("个人风采图片上传大小超过4M");   
  108.             logger.debug("**************************************************");   
  109.             return "redirect:/account/personal_style?exception=imgtoolong";   
  110.         }   
  111.   
  112.         this.model.get().addAttribute("title",   
  113.                 "个人风采_账号设置_" + CMConfig.getProperty("default.title"));   
  114.         return "redirect:/account/personal_style";   
  115.     }  

抱歉!评论已关闭.