springboot实现图片上传到static目录后,访问不到,需要重启服务后才可访问,解决方案就是设置虚拟目录
1 @SuppressWarnings("all") 2 @Configuration 3 public class UserIcoUploadConfig implements WebMvcConfigurer { 4 5 //addResourceHandlers 6 7 /** 8 * 将D:\\upload下的文件映射到当前项目/upload/下 9 * 10 * @param registry 11 */ 12 @Override 13 public void addResourceHandlers(ResourceHandlerRegistry registry) { 14 //addResourceHandler()里配置需要映射的文件夹,此处代表映射文件夹user下的所有资源。 15 //addResourceLocations()配置文件夹在系统中的路径,使用绝对路径,格式为“file:你的路径” 16 registry.addResourceHandler("/userIco/**"). 17 addResourceLocations("file:C:\\Project\\test01\\src\\main\\resources\\static\\userIco\\"); 18 } 19 }
1 /** 2 * 上传用户头像 3 * @param file 4 * @return http://localhost:8080/itkb/user/uploadUserIco 5 */ 6 @PostMapping(value = "uploadUserIco") 7 public String uploadUserIco(MultipartFile file) throws IOException { 8 // 图片存储路径 9 String path = "C:\\Project\\test01\\src\\main\\resources\\static\\userIco\\"; 10 if (!new File(path).exists()) { 11 new File(path).mkdirs(); 12 } 13 String fileName = System.currentTimeMillis() + ".jpg"; 14 file.transferTo(new File(path+fileName)); 15 return "http://locaohost:8080/userIco/"+fileName; 16 }
标签:springboot,虚拟目录,userIco,static,registry,file,path,fileName,上传 From: https://www.cnblogs.com/lwl80/p/16871195.html