Anton Charov Asked:2020-09-10 21:03:51 +0000 UTC2020-09-10 21:03:51 +0000 UTC 2020-09-10 21:03:51 +0000 UTC 如何组织服务器上的图像存储? 772 如何更轻松快捷地组织服务器上 jpg 文件的存储?spring boot 上有一个小后端,它返回带有“img”的 json:例如“https://server/1.jpg”。如何通过 https 组织对文件的访问? java 2 个回答 Voted Alex K 2020-09-11T13:33:33Z2020-09-11T13:33:33Z 您可以在同一个 Spring Boot 中设置一个包含资源的目录 @Configuration @EnableWebMvc public class MvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry .addResourceHandler("/images/**") .addResourceLocations("/images/"); } } 访问将是这样的:https://server/images/1.jpg Best Answer Anton Charov 2020-09-12T05:39:58Z2020-09-12T05:39:58Z 做了一个单独的控制器 @GetMapping(value = "/{filename}", produces = MediaType.IMAGE_PNG_VALUE) public @ResponseBody byte[] getImg(@PathVariable("filename") String filename) { try { InputStream in = new FileInputStream(contentPath + filename); if (in == null) { throw new NoContentException(contentPath + filename); } return IOUtils.toByteArray(in); } catch (FileNotFoundException e) { throw new NoContentException(contentPath + filename); } catch (IOException e) { throw new NoContentException(contentPath + filename); } }
您可以在同一个 Spring Boot 中设置一个包含资源的目录
访问将是这样的:https://server/images/1.jpg
做了一个单独的控制器