web项目中,上传的文件有时会不能实时更新,必须要先更新图片文件夹,并再次刷新才可以显示。
解决方法:改变图片文件的路径,将绝对路径改为相对路径
@Controller
@RequestMapping("/user")
public class FileController {
@RequestMapping("/all")
public String Fileload(MultipartFile img,Model m,HttpServletRequest request) throws Exception {
String path = request.getSession().getServletContext().getRealPath("服务器资源的相对路径");
/*获取到上传文件的原始名称*/
String filename = img.getOriginalFilename();
File file = new File(path, filename);
img.transferTo(file);
m.addAttribute("filename",filename);
return "welcome";
}
}