editor渲染上加入:
imageUpload: true, //这个配置允许图片上传
imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"], //图片上传过滤器
imageUploadURL: "upload/upload?path=F:/article/temp/", //上传接口
这样点击添加图片就会出现 "本地上传" 字样。接下来写后台:
@RequestMapping("/upload")
@ResponseBody
public Map<String, Object> upload(@RequestParam("editormd-image-file") MultipartFile file, @RequestParam("path") String path){
Map<String, Object> jsonMap = new HashMap<String, Object>();
String trueFileName = file.getOriginalFilename(); //获取源文件名
String suffix = trueFileName.substring(trueFileName.lastIndexOf(".")); //后缀
String fileName = System.currentTimeMillis()+"_"+RandomName.randomOname(10)+suffix; //数据库中文件名
getRealPath(path); //创建目录
try {
//复制文件
InputStream is = file.getInputStream();
OutputStream os = new FileOutputStream(new File(path,fileName));
IOUtils.copy(is, os);
os.close();
is.close();
jsonMap.put("success", 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
jsonMap.put("success", 0);
}
jsonMap.put("url", "art/temp/"+fileName);
jsonMap.put("message", "图片上传成功!");
return jsonMap;
}
//获取目标文件的绝对路径(通过相对路径)
public String getRealPath(String path) {
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
return path;
}
注意:imageUploadURL处是拼接的上传路径 ?path=F:/article/temp/