
@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);
}
经过测试:
在
File.java
里可以看到其构造方法的源码:其中
path
就是影响到结果的地方,fs
在Windows
下是一个WinNTFileSystem
的实例,其normalize
方法的源码如下:具体为什么
/
会导致结果的不同,现在我没时间追下去,有兴趣你可以自行看一下,这个文件在java.io.WinNTFileSystem
那你就检查Tomcat
(或者其他Server
) 的配置文件看一下webroot
指向了哪里