servlet 无法从 http 服务器读取图像

发布于 2025-01-04 22:08:03 字数 1146 浏览 1 评论 0原文

我有一个通过 http url 来自远程服务器的图像。我想对此进行一些图像分析,所以我想在我的服务器端java代码中读取图像内容。该代码在 tomcat 服务器内部运行。

    public ImageTrimmer(String urlPath) {
        imageSrc = urlPath;
        try {
           Image img = ImageIO.read(new URL(imageSrc));  // throws the exception
            img.getRGB(0, 0);
        } catch (IOException e) {
            throw new RuntimeException( "Problem reading image", e );
        }
    }

图像的 url 可以是任何返回图像的有效 url。示例 URL 路径为 -“http://build.ford.com/dig/Ford/E-Series Cargo Van/2012/BP3TT-FULL-EXT/Image[|Ford|E-Series Cargo Van|2012|1|” 1.|.E1E..YZ...89E.BS1.CB1.]/EXT/1/vehicle.png”。

这是我在阅读图像时遇到的异常

java.io.IOException: Server returned HTTP response code: 505 for URL: http://build.ford.com/dig/Ford/E-Series Cargo Van/2012/BP3TT-FULL-EXT/Image[|Ford|E-Series Cargo Van|2012|1|1.|.E1E..YZ...89E.BS1.CB1.]/EXT/1/vehicle.png

        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
        at java.net.URL.openStream(URL.java:1010)
        at javax.imageio.ImageIO.read(ImageIO.java:1367)

任何成功阅读此图像的帮助都将非常有帮助。

I have an image coming from remote server through http url. I want to do some image analysis on this, so I want to read the image content in my server side java code. This code is running inside the tomcat server.

    public ImageTrimmer(String urlPath) {
        imageSrc = urlPath;
        try {
           Image img = ImageIO.read(new URL(imageSrc));  // throws the exception
            img.getRGB(0, 0);
        } catch (IOException e) {
            throw new RuntimeException( "Problem reading image", e );
        }
    }

The url for image can be any valid url that returns image. An example url path is - "http://build.ford.com/dig/Ford/E-Series Cargo Van/2012/BP3TT-FULL-EXT/Image[|Ford|E-Series Cargo Van|2012|1|1.|.E1E..YZ...89E.BS1.CB1.]/EXT/1/vehicle.png".

Here is the exception I get while reading the image

java.io.IOException: Server returned HTTP response code: 505 for URL: http://build.ford.com/dig/Ford/E-Series Cargo Van/2012/BP3TT-FULL-EXT/Image[|Ford|E-Series Cargo Van|2012|1|1.|.E1E..YZ...89E.BS1.CB1.]/EXT/1/vehicle.png

        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
        at java.net.URL.openStream(URL.java:1010)
        at javax.imageio.ImageIO.read(ImageIO.java:1367)

Any help to read this image successfully will be veryhelpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2025-01-11 22:08:03

当在浏览器中输入时,浏览器会帮助您完成一些工作。为了访问它,请首先尝试在代码中对 url 进行编码(以转义某些特定字符,例如 / 、空白)。一种可选 API 是

URLEncoder.encode(arg0)

When typing in browser, the browser help u do some work. In order to access it, try encode the url in you code(to escape some specific character like / ,blank) first. One optional API is,

URLEncoder.encode(arg0)

听不够的曲调 2025-01-11 22:08:03

Tomcat 可能因 URL 中的空格而卡住。尝试将 String urlPath 编码为 Tomcat 可以理解的内容。

String encodedPathURL;
try
{
  encodedPathURL = java.net.URLEncoder().encode(urlPath, "UTF-8");
}
catch(UnsupportedEncodingException exception)
{
  //Exception handling here
}

并在调用 ImageIO.read(new URL(encodedPathURL)) 时使用正确编码的 StringencodedPathURL。

It might be that Tomcat is choking on the whitespace in the URL. Try encoding the String urlPath to something Tomcat can understand.

String encodedPathURL;
try
{
  encodedPathURL = java.net.URLEncoder().encode(urlPath, "UTF-8");
}
catch(UnsupportedEncodingException exception)
{
  //Exception handling here
}

and using the properly encoded String encodedPathURL when calling ImageIO.read(new URL(encodedPathURL)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文