@RequestMapping(value = "/upload_img",method = {RequestMethod.GET, RequestMethod.POST},produces = "application/json; charset=utf-8")
@ResponseBody
public String upload_img(MultipartFile file,HttpServletRequest request) throws Exception{
String path="/upload/news";
//创建文件
File dir=new File(path);
if(!dir.exists()){
dir.mkdirs();
}
String id = SysUtil.getUUID();
String fileName=file.getOriginalFilename();
String img=id+fileName.substring(fileName.lastIndexOf("."));//zhao.jpg
FileOutputStream imgOut=new FileOutputStream(new File(dir,img));//根据 dir 抽象路径名和 img 路径名字符串创建一个新 File 实例。
imgOut.write(file.getBytes());//返回一个字节数组文件的内容
imgOut.close();
Map<String, String> map=new HashMap<String, String>();
map.put("path",img);
return JSON.toJSONString(map);
}
打印调试法:
在写文件前后打控制台输出,写图片换成写个
txt
看一下结果。