标签:上传
@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