spring mvc 图片上传
/** * 保存个人风采 * * @throws IOException * */@RequestMapping(value = "/saveImgs1")public String saveImgs(ModelMap modelMap) {/* 权限验证 */int uid = this.user.get().getUid();if (this.user.get().getUid() <= 0) {return "redirect:/error/index?code=auth.unlogin";}// 允许上传的文件格式的列表JPG、JPEG、GIF、BMPfinal String[] allowtype = new String[] { ".JPG", ".JPEG", ".GIF",".BMP", ".PNG", ".jpg", ".jpeg", ".gif", ".bmp", ".png" };try {String realPath = CMConfig.getProperty("newstempurl"); // 文件上传的绝对路径File fileDir = new File(realPath);if (!fileDir.exists()) {fileDir.mkdirs();}request.get().setCharacterEncoding("UTF-8");MultipartRequest mr = new MultipartRequest(request.get(), realPath,4194304, "UTF-8");// mr.getString filename = "";String description = "";Enumeration filesname = mr.getFileNames();Enumeration filesdesc = mr.getParameterNames();String newImgPath = ""; // 新图片路径String url = ""; // 保存图片的urlwhile (filesname.hasMoreElements()) {String name = (String) filesname.nextElement();String dc = (String) filesdesc.nextElement();filename = mr.getFilesystemName(name);description = mr.getParameter(dc);// filedName = (String) filesname.nextElement();// 文件文本框的名称File uploadFile = mr.getFile(name);if (null != uploadFile && uploadFile.length() > 0) {String imgPath = uploadFile.getName();// imgPath为原文件名int idx = imgPath.lastIndexOf(".");// 文件后缀String extention = imgPath.substring(idx);java.util.Date dt = new java.util.Date(System.currentTimeMillis());SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");Long now = System.currentTimeMillis();String time = fmt.format(dt) + String.valueOf(now);// 新的文件名(日期+后缀)newImgPath = this.user.get().getUid() + "_" + time+ extention;int allowFlag = 0;int allowedExtCount = allowtype.length;for (; allowFlag < allowedExtCount; allowFlag++) {if (allowtype[allowFlag].equals(extention))break;}if (allowFlag == allowedExtCount) {String message = "";for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++) {message += "*" + allowtype[allowFlag] + " ";}logger.debug(extention + "message:'请上传以下类型的文件"+ message + "'");return "redirect:/account/personal_style?exception=imgtoolong";}File f = new File(fileDir + "/" + newImgPath);uploadFile.renameTo(f);Date d = new Date(now);String year = DateUtil.dateToString(d, "yyyy");String month = DateUtil.dateToString(d, "MM");String day = DateUtil.dateToString(d, "dd");url = CMConfig.getProperty("image.upload.url") + "/" + year+ "/" + month + "/" + day + "/" + newImgPath;FileMove.move(CMConfig.getProperty("newstempurl")+ newImgPath, CMConfig.getProperty("newsurl"),newImgPath);}}UDBUserDao userDao = AppContext.getBean("userDao", UDBUserDao.class);if (url.trim().length() == 0) {return "redirect:/account/personal_style?exception=imgnotnull";} else {if (userDao.initSelfStyle(uid, url) > 0) {// 增加积分ScoreService scoreService = AppContext.getBean("scoreService", ScoreService.class);scoreService.addScore(this.user.get().getUid(),ScoreRuleType.ADD_STYLE, 0);return "redirect:/account/personal_style?exception=success";} else {return "redirect:/account/personal_style?exception=error";}}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {logger.debug("**************************************************");logger.debug("个人风采图片上传大小超过4M");logger.debug("**************************************************");return "redirect:/account/personal_style?exception=imgtoolong";}this.model.get().addAttribute("title","个人风采_账号设置_" + CMConfig.getProperty("default.title"));return "redirect:/account/personal_style";}?