类路径资源【static/pixel.png】无法打开,因为它不存在(Heroku)

huangapple go评论87阅读模式
英文:

class path resource [static/pixel.png] cannot be opened because it does not exist (Heroku)

问题

我正在使用 Jhipster 开发一个 Spring Boot 应用程序,尝试在电子邮件中添加像素。像素图像保存在 resources/static 文件夹中。我的像素链接:

    @GetMapping("/email-status/OPENED/{customer}")
    @Timed
    public void mailOpened(@PathVariable String customer, HttpServletResponse response) throws IOException {

        //代码...

        InputStream in = new ClassPathResource("/static/pixel.png").getInputStream();

        response.setContentType(MediaType.IMAGE_PNG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
    }

静态文件夹资源浏览器

当我从浏览器使用 localhost:8080/api/email-status/OPENED/... 调用 API 时,像素图像会显示出来。

但是当我从我的 域名 https://app.mydomain.com/api/email-status/OPENED/123 调用时(我在 Heroku 上进行托管),我会得到以下错误:

java.io.FileNotFoundException: 无法打开类路径资源 [static/pixel.png],因为它不存在。

有人知道为什么会发生这种情况吗?

英文:

Im developing a spring boot application using Jhipster, and Im trying to add pixel to email.
The pixel image saved on resources/static folder.
My pixel link:

    @GetMapping("/email-status/OPENED/{customer}")
    @Timed
    public void mailOpened(@PathVariable String customer, HttpServletResponse response) throws IOException {

        //code...

        InputStream in = new ClassPathResource("/static/pixel.png").getInputStream();

        response.setContentType(MediaType.IMAGE_PNG_VALUE);
        IOUtils.copy(in, response.getOutputStream());
    }

The static folder explorer

When i call the api from the browser with localhost:8080/api/email-status/OPENED/...
the pixel image is displayed.

But when I call from my Domain https://app.mydomain.com/api/email-status/OPENED/123

(I'm using Heroku for hosting)
I get this error

> java.io.FileNotFoundException: class path resource [static/pixel.png] cannot be opened because it does not exist

Anyone know why does this happen?

答案1

得分: 1

好的,以下是翻译的内容:

我认为 localhost 使用生成的类,而域使用 jar 文件。在后一种情况下,图像位于 jar 文件内,这就是为什么无法找到它。

根据文档中对于 ClassPathResource 的说明:

> 如果类路径资源位于文件系统中,则支持解析为 java.io.File,但不支持位于 JAR 文件中的资源

也许你应该考虑使用:

YourClassName.class.getResourceAsStream("/static/pixel.png")

一个关于使用资源的非常有用的链接是 "如何使用图标"

英文:

It seems to me that the localhost uses the generated classes where the domain uses the jar. In the latter case the image is inside the jar and that is why cannot be found.

As the document states for ClassPathResource:

> Supports resolution as java.io.File if the class path resource resides
> in the file system, but not for resources in a JAR

Maybe you should consider using

YourClassName.class.getResourceAsStream("/static/pixel.png")

A very useful link about using resources is the "How to Use Icons"

答案2

得分: 0

如果您将应用部署在Web服务器上以便从您的域访问它,则只有当图像也存储在同一服务器上时,它才能访问该图像。

在这种情况下,您需要将图像复制到服务器上的适当文件夹中。

英文:

In case you deployed your app on a webserver in order to access it from your domain, it could only access the image if it is also stored on the same server.

In that case you'd have to copy the image in the appropriate folder on your server.

答案3

得分: 0

So I did not find any answer, The image is exists in the files, but yet couldn't use any Local path resource solution ..

I have decided to host the image and use the link instead of a static image, This is my working solution

InputStream in = new URL("https://some-host/12345/pixel.png").openStream();
response.setContentType(MediaType.IMAGE_PNG_VALUE);
IOUtils.copy(in, response.getOutputStream());

And one more thing that was a must for me is also, the img tag should contain all those params

<img src="image_url(the pixel image)" alt="Logo" title="Logo" style="display:block" width="1" height="1" />
英文:

So I did not find any answer, The image is exists in the files, but yet couldn't use any Local path resource solution ..

I have decided to host the image and use the link instead of static image, This is my working solution

InputStream in = new URL(&quot;https://some-host/12345/pixel.png&quot;).openStream();
response.setContentType(MediaType.IMAGE_PNG_VALUE);
IOUtils.copy(in, response.getOutputStream());

And one more thing that was must for me is also, the img tag should contain all those params

&lt;img src=&quot;image_url(the pixel image)&quot; alt=&quot;Logo&quot; title=&quot;Logo&quot; style=&quot;display:block&quot; width=&quot;1&quot; height=&quot;1&quot; /&gt;

huangapple
  • 本文由 发表于 2020年8月27日 20:25:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63615975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定