jsp:jsp页面:
@RequestMapping("core/upload!uploadFile.action") public void uploadFile(@RequestParam(value = "uploadify", required = false) MultipartFile uploadify,HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) throws Exception { @SuppressWarnings("deprecation") String savePath = request.getRealPath("/")+ "/" + "upload/"; String saveUrl = "upload/"; HashMapextMap = new HashMap (); extMap.put("file", "pdf,rar,zip,7z,gif,jpg,jpeg,png,bmp,doc,docx,xlsx,xls"); //extMap.put("image", "gif,jpg,jpeg,png,bmp"); extMap.put("csv", "csv"); int uploadMaxSize =Integer.valueOf(optionService.getByOptionName(CoreValue.OPTION_UPLOAD_MAX_SIZE).getOptionValue()); long maxSize = Long.valueOf(String.valueOf(uploadMaxSize)).longValue(); String maxSizeKb = StringUtil.formatNumber(new Double(maxSize / 1024L),"0"); response.setContentType("text/html; charset=UTF-8"); String jsonString = ""; if (!ServletFileUpload.isMultipartContent(getRequest())) { jsonString = getError("请选择文件。"); response.getWriter().println(jsonString); return; } File uploadDir = new File(savePath); if (!uploadDir.isDirectory()) { jsonString = getError(savePath + "上传目录不存在。"); response.getWriter().println(jsonString); return; } if (!uploadDir.canWrite()) { jsonString = getError("上传目录没有写权限。"); response.getWriter().println(jsonString); return; } String dirName = getRequest().getParameter("dir"); if (dirName == null) { dirName = "file"; } if (!extMap.containsKey(dirName)) { jsonString = getError("目录名不正确。"); response.getWriter().println(jsonString); return; } savePath = savePath + dirName + "/"; saveUrl = saveUrl + dirName + "/"; File saveDirFile = new File(savePath); if (!saveDirFile.exists()) { saveDirFile.mkdirs(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); String ymd = sdf.format(new Date()); savePath = savePath + ymd + "/"; saveUrl = saveUrl + ymd + "/"; File dirFile = new File(savePath); if (!dirFile.exists()) { dirFile.mkdirs(); } String fileName = uploadify.getOriginalFilename(); long fileSize = uploadify.getSize(); if (fileSize > maxSize) { jsonString = getError("上传文件大小超过限制。最大为" + maxSizeKb + "kb"); response.getWriter().println(jsonString); return; } String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); if (!Arrays.asList(((String) extMap.get(dirName)).split(",")).contains(fileExt)) { jsonString = getError("不允许的上传文件类型。"); response.getWriter().println(jsonString); return; } SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String newFileName = df.format(new Date()) + "_"+ new Random().nextInt(1000) + "." + fileExt; File uploadedFile = null; try { uploadedFile = new File(savePath, newFileName); if(uploadedFile.exists()){ } if(!uploadedFile.exists()){ uploadedFile.mkdirs(); } uploadify.transferTo(uploadedFile); } catch (Exception e) { jsonString = getError("上传文件失败。"); response.getWriter().println(jsonString); return; } JSONObject obj = new JSONObject(); obj.put("error", Integer.valueOf(0)); obj.put("url", saveUrl + newFileName); obj.put("newFileName", newFileName); obj.put("fileName", fileName); jsonString = obj.toJSONString(); response.getWriter().println(jsonString); } private String getError(String message) { JSONObject obj = new JSONObject(); obj.put("error", Integer.valueOf(1)); obj.put("message", message); return obj.toJSONString(); }