【问题标题】:Returning a image with ServletContext使用 ServletContext 返回图像
【发布时间】:2017-11-07 18:50:21
【问题描述】:

我正在尝试使用 ServletContext 返回图像,但出现 500 错误并且控制台显示:

java.lang.NullPointerException: null 在 org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2146) 在 org.apache.commons.io.IOUtils.copy(IOUtils.java:2102) 在 org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123) 在 org.apache.commons.io.IOUtils.copy(IOUtils.java:2078) 在 org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:721)

配置:

@Configuration
public class ImageConfiguration {

    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(byteArrayHttpMessageConverter());
    }

    @Bean
    public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
        ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
        arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
        return arrayHttpMessageConverter;
    }

    private List<MediaType> getSupportedMediaTypes() {
        List<MediaType> list = new ArrayList<MediaType>();
        list.add(MediaType.IMAGE_JPEG);
        list.add(MediaType.IMAGE_PNG);
        list.add(MediaType.APPLICATION_OCTET_STREAM);
        return list;
    }
}

服务:

@Service
public class ImageService {

    @Autowired
    ServletContext servletContext;

    public byte[] getRankImage (String id) throws IOException {

        byte[] b;

            InputStream in;


        if (id.equals("0")) {
            in = servletContext.getResourceAsStream("images/level-0.png");
            return IOUtils.toByteArray(in);

宁静的服务:

@RequestMapping(value = "/level/{id}", method = RequestMethod.GET)
    public ResponseEntity<byte[]> getImage(@PathVariable("id") String id) {
        byte[] imageBytes;

        try {
            imageBytes = imageService.getRankImage(id);
            return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(imageBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

图像位于resources -&gt; images -&gt; {image_name},当我查看war 文件时,我确认它们在其中。我不确定我是否做错了,或者我需要以不同的方式处理路径。

---------更新1-------

我获取了war文件并在apache中手动部署了它,这是输出:

【问题讨论】:

  • 那么,InputStream 实例 in 在这里为空?

标签: spring-boot


【解决方案1】:

ImageService 类中,您自动连接了ServletContext 实例。

Spring 容器不会自动装配 servletContext,因为它不是 Spring bean。 ServletContext 来自 javax.servlet 包。

您可以使用setter方法注入实例,如下所示。

@Service
public class ImageService implements ServletContextAware{

     private ServletContext context;

     public void setServletContext(ServletContext servletContext) {
        this.context = servletContext;
     }

     // use `context` object to get the image and pass to IOUtils.toByteArray method
}

通过实现ServletContextAware 接口,Spring 会为你注入它。

理想情况下,我们不应该在Controller 层之外注入ServletContext 实例。在这里,我们在服务层注入,这不是推荐的方法。

希望这会有所帮助!

【讨论】:

  • java.lang.NullpointerEception: null 是同样的错误。这些文件位于recources -&gt; images -&gt; {image_name}
  • if (id.equals("0")) { in = servletContext.getResourceAsStream("images/level-0.png"); return IOUtils.toByteArray(in); }
  • 不要认为它适用于 spring-boot。我认为 servlet 上下文资源与 war 一起使用。我认为他们在/src/main/webapp 而不是/src/main/resources 工作。您是否将其作为 jar 运行?
  • 您是否验证了图像是否位于 jar 内的同一位置?你爆过检查过吗?
  • 我刚刚更新了帖子。我没有看到我通常在战争文件中看到的图像。格式看起来不对 - 我正在使用 spring boots 内部 apache 服务器
【解决方案2】:

来自http://www.xyzws.com/servletfaq/how-to-use-servletcontextgetresourceasstreamjavalangstring-path/18,行

in = servletContext.getResourceAsStream("images/level-0.png");

应该在图片之前有一个/,并且路径应该是相对于当前上下文根目录(WEB-INF 的父节点)。

【讨论】:

  • 我尝试添加/并将图像也放入webapps,它仍然返回null。
  • 所以你的图片在 WEB-INF/../images/?
  • @Drew1208,这(带有双点)有效吗?如果是,请接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 2019-10-30
  • 2021-07-30
  • 2018-12-17
相关资源
最近更新 更多