@RequestMapping("/uploadImage")@ResponseBody // 这里upfile是config.json中图片提交的表单名称public Map<String, String> uploadImage(@RequestParam("upfile") CommonsMultipartFile upfile,HttpServletRequest request) throws IOException {// 文件原名称String fileName = upfile.getOriginalFilename();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");String formatDay = simpleDateFormat.format(new Date());// 为了避免重复简单处理String nowName = formatDay + "_"+new Date().getTime() + "_" + fileName;if (!upfile.isEmpty()) {String ueditorImagePath = PropertyUtil.readValue(Const.ueditorImagePath);// 上传位置路径String path0 = ueditorImagePath + "/" +formatDay+"/"+ nowName;// 按照路径新建文件java.io.File newFile = new java.io.File(path0);java.io.File newFile1 = new java.io.File(ueditorImagePath+ "/" +formatDay);if (!newFile1.exists()){boolean mkdir = newFile1.mkdirs();}// 复制FileCopyUtils.copy(upfile.getBytes(), newFile);}// 返回结果信息(UEditor需要)Map<String, String> map = new HashMap<String, String>();// 是否上传成功map.put("state", "SUCCESS");// 现在文件名称map.put("title", nowName);// 文件原名称map.put("original", fileName);// 文件类型 .+后缀名map.put("type", fileName.substring(upfile.getOriginalFilename().lastIndexOf(".")));// 文件路径map.put("url", "/kentra/file/getImage.do?imgName="+nowName);// 文件大小(字节数)map.put("size", upfile.getSize() + "");return map;}
本文出自 “JianBo” 博客,请务必保留此出处http://jianboli.blog.51cto.com/12075002/1980250
上传文件
原文地址:http://jianboli.blog.51cto.com/12075002/1980250