@RequestMapping(value="/uploadImage",method = RequestMethod.POST)
public void uploadImage(@RequestParam("file") MultipartFile file,PrintWriter pw,ModelMap modelMap,HttpServletRequest req) throws IOException {
String type = file.getOriginalFilename().split("\\.")[1].toLowerCase();
String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
int num=t.indexOf(".metadata");
String path=t.substring(1,num).replace('/', '\\')+"project001\\WebContent\\static\\admin\\common\\upload\\";
String fileName = "logo"+new Date().getTime()+"."+type;
if(type.equals("png") || type.equals("jpg") || type.equals("gif") || type.equals("jpeg")){
File f = new File(path+fileName);
FileOutputStream out = new FileOutputStream(f);
out.write(file.getBytes());
modelMap.put("msg", "上传成功");
}else{
modelMap.put("msg", "上传失败,仅支持png,jpg,gif,jpeg格式文件.");
}
pw.print("{\"msg\":\"上传成功\",\"url\":\""+getBasePath(req)+"static/admin/common/upload/"+fileName+"\"}");
}
这是我的代码。、
要刷新eclipse的项目名才能显示图片在页面上。。有什么办法解决吗?
我觉得可以试试FileOutputStream out = new FileOutputStream(f); 把这个流flush and close一下
我用spring boot rest方式测试其实和close应该没什么关系,从要等很久和刷新eclipse之后能查看,应该是这个文件上传成功只保存在了eclipse项目本地,等待时间就是重新部署的问题了。