servlet 无法从 http 服务器读取图像
我有一个通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当在浏览器中输入时,浏览器会帮助您完成一些工作。为了访问它,请首先尝试在代码中对 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)
Tomcat 可能因 URL 中的空格而卡住。尝试将 String urlPath 编码为 Tomcat 可以理解的内容。
并在调用 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.
and using the properly encoded String encodedPathURL when calling ImageIO.read(new URL(encodedPathURL)).