application.yml先配置路径
虚拟路径配置
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//文件磁盘图片url 映射
//配置server虚拟路径,handler为前台访问的目录,locations为files相对应的本地路径
// registry.addResourceHandler("/attached/**").addResourceLocations("file:D:/img/attached/");
registry.addResourceHandler("/attached/**").addResourceLocations("file:/var/local/attached/");
}
}
导入jar
前端(其它配置可以去kinEditor官网)
Controller
@RestController
public class UtilsController {
@Value("${uploadPrefix}")
private String uploadPrefix;
private String getError(String message) {
JSONObject obj = new JSONObject();
obj.put("error", 1);
obj.put("message", message);
return obj.toJSONString();
}
@RequestMapping("/uploadJson")
public void uploadJson(HttpServletRequest request, HttpServletResponse response, String dir) throws Exception {
response.setContentType("application/json; charset=UTF-8");
PrintWriter out = response.getWriter();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//文件保存目录路径
String savePath = uploadPrefix + "/attached/";
//文件保存目录URL
String saveUrl = uploadPrefix + "/attached";
System.out.println(savePath);
System.out.println(saveUrl);
//定义允许上传的文件扩展名
HashMap<String, String> extMap = new HashMap<String, String>();
extMap.put("image", "gif,jpg,jpeg,png,bmp");
extMap.