您的位置:首页 > 健康 > 养生 > Springboot 上传图片及访问

Springboot 上传图片及访问

2024/12/23 12:24:00 来源:https://blog.csdn.net/songqping/article/details/139495572  浏览:    关键词:Springboot 上传图片及访问

1、yml配置

下面的服务器根据自己所用的进行调整即可

upload:file:# 这是linux服务器的上传路径#    location: file:/mnt/www/pp/test_ai/image#这是windows服务器的上传路径location: file:d://image/#这是访问的虚拟路径path: /temp-image/**
fileServer:
#这是访问图片的服务url: http://localhost:9099/test_api

2、上传controller

此方法中ObjectResponse是本人自定义的返回类,大家可以根据自己的来进行编写。

@RequestMapping({"/file"})
@RestController
@CrossOrigin(origins = {"*"}, maxAge = 3600)
/* loaded from: CarouseImageController.class */
public class FileUploadController {@Value("${upload.file.location}")private String fileLocation;@Value("${upload.file.path}")private String filePath;@Value("${fileServer.url}")private String fileServer;@PostMapping({"/importPicture"})@ResponseBody@CrossOrigin(origins = {"*"})public ObjectResponse importPicture(@RequestParam("file") MultipartFile file) throws FileNotFoundException {String filename = UUID.randomUUID() + ((String) Objects.requireNonNull(file.getOriginalFilename())).substring(file.getOriginalFilename().lastIndexOf("."));File filepath = new File(this.fileLocation.replaceAll("file:", "") + filename);String.valueOf(filepath);try {file.transferTo(filepath);System.out.println("文件已存储成功,路径:" + filepath.getPath());} catch (IOException e) {e.printStackTrace();}return new ObjectResponse (Boolean.TRUE.booleanValue(), "操作成功!", (this.filePath + filename).replaceAll("\\*", ""));}

}

3、添加配置类(此类必须要有,不然是访问不了的)

@Configuration
public class ProjectWebMvcConfigurer implements WebMvcConfigurer {@Value("${upload.file.location}")private String fileLocation;@Value("${upload.file.path}")private String filePath;@BeanTokenInterceptor paramInterceptor() {return new TokenInterceptor();}public void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler(new String[]{"/static/**"}).addResourceLocations(new String[]{"classpath:/static/"});registry.addResourceHandler(new String[]{"/templates/**"}).addResourceLocations(new String[]{"classpath:/templates/"});registry.addResourceHandler(new String[]{this.filePath}).addResourceLocations(new String[]{this.fileLocation});}public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(paramInterceptor()).addPathPatterns(new String[]{"/**"}).excludePathPatterns(new String[]{"/static/**"});}public void addCorsMappings(CorsRegistry registry) {System.out.println("----------------------");registry.addMapping("/**").allowedOrigins(new String[]{"*"}).allowCredentials(true).allowedMethods(new String[]{AdminCommonConstant.RESOURCE_REQUEST_METHOD_GET, AdminCommonConstant.RESOURCE_REQUEST_METHOD_POST, AdminCommonConstant.RESOURCE_REQUEST_METHOD_DELETE, AdminCommonConstant.RESOURCE_REQUEST_METHOD_PUT, "OPTIONS"}).maxAge(3600L);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com