gpt4 book ai didi

Java URL java.net.ConnectException 错误(Ping 有效,其他 URL 有效)

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:52 30 4
gpt4 key购买 nike

我正在编写一个程序,它从列表中读取一些 ID,从中找出不同的 URL,然后将图像保存到我的 C: 驱动器中。

如果我在浏览器中导航到图像 URL,它们就会起作用。此外,如果我尝试从不同的服务器获取图像的 URL,则该程序完全可以工作。问题是当我尝试连接到此 URL 上的特定图像时,它似乎不起作用。奇怪的是,当我 ping URL 时,丢包率为 0%。此外,我的浏览器没有使用特殊的代理设置,这会导致它通过 Java 程序运行。

以下错误/输出的原因可能是什么?

Job started ...
1) AC_0A47_EXT1.jpg 2/0/47/307/398/HA47H01_H.jpg
Exception #3 java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at pullVFMImagesFromVFMURLs.writeImage(pullVFMImagesFromVFMURLs.java:99)
at pullVFMImagesFromVFMURLs.pullvfmimagesfromvfmurls(pullVFMImagesFromVFMURLs.java:48)
at pullVFMImagesFromVFMURLs.main(pullVFMImagesFromVFMURLs.java:21)

更多信息:

ID列表(MissingImagez.txt)如下:

AC_0A47_EXT1.jpg|2/0/47/307/398/xxx.jpg
AC_0C09_EXT1.jpg|3/0/44/130/589/xyz.jpg
AC_0C16_RM1.jpg|3/0/44/602/895/zzz.jpg
AC_0C17_BAR1.jpg|3/0/45/284/10/www.jpg

对应的代码如下:

  public static void main(String[] args)
{
pullImagesFromURLs processor = new pullImagesFromURLs();

String passParam = null;

for (int i = 0; i < args.length; i++)
passParam = (args[i].toString().toUpperCase());

processor.pullvfmimagesfromvfmurls(passParam);
}

public void pullvfmimagesfromvfmurls(String passParam)
{
System.out.println("Job started ...");

try
{
boolean bOK = false;
String sOrigName = "";
String sURL = "";
int iCount = 0;
int iInt = 0;

try
{
BufferedReader in = new BufferedReader(new FileReader("/tmp/missingImagez.txt"));
String str;

while ((str = in.readLine()) != null)
{
iInt = str.indexOf("|");
sOrigName = str.substring(0,iInt);
sURL = str.substring(iInt+1);
iCount ++;
System.out.println(iCount + ") " + sOrigName + " " + sURL);
bOK = writeImage(sOrigName, sURL);
}

try
{
if (in != null)
in.close();
}
catch (Exception e)
{
}

}
catch (IOException e)
{
System.out.println("Exception #1 " + e);
}
}

catch (Exception e)
{
System.out.println("Exception #2 " + e);
System.exit(-666);
}

System.out.println("Job completed");

System.exit(-666);

}
private static boolean writeImage(String origName, String vfmURL)
{

boolean bOK = true;
String sPath = "C:/images/img2754/";

try
{
final String vfmURLPrefix = new String("http://www.blah.blah2.com/imageRepo/");

BufferedInputStream stream = null;
FileOutputStream destination = null;
URLConnection urlc = null;
int readSize = 1024 * 50;
int bytes_read;
URL url = null;

url = new URL(vfmURLPrefix + vfmURL);
urlc = url.openConnection();
urlc.setDoOutput(true);
stream = new BufferedInputStream(url.openStream());
System.out.println(url.toString());
destination = new FileOutputStream(sPath+origName);
byte[] buffer = new byte[readSize];

while(true)
{

bytes_read = stream.read(buffer);
if (bytes_read == -1) {break;}
destination.write(buffer, 0, bytes_read);
stream.close();
}

if(stream != null)
stream.close();

if(destination != null)
destination.close();

}

catch(Exception e)
{
System.out.println("Exception #3 " + e);
}
return bOK;

}

尝试使用 Apache HttpComponents。得到以下输出:好吧——我试过了……现在我明白了:

Exception #3     org.apache.http.conn.HttpHostConnectException: Connection to http://www.blah.blah2.com/ refused. 
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at pullVFMImagesFromVFMURLs.writeImage(pullVFMImagesFromVFMURLs.java:100)
at pullVFMImagesFromVFMURLs.pullvfmimagesfromvfmurls(pullVFMImagesFromVFMURLs.java:55)
at pullVFMImagesFromVFMURLs.main(pullVFMImagesFromVFMURLs.java:28)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
... 9 more

最佳答案

编辑2

您的代码在读取循环中存在错误。 InputStream.read不需要返回正在接收的整个字节流。 (可能尚未全部可用。)按照您的编码方式,输入流通常会过早关闭。这会导致与您发布的错误不同的错误(“流已关闭”)。

无论如何,修复循环后,我能够运行您的代码来下载此图像,没有任何问题。

原创

顺便说一句,你的代码有点令人困惑。特别是,您的代码创建 URLConnection,但直接从 URL 打开流。 URLConnection基本没用过。这也很令人困惑,因为您调用了 setDoOutput,但没有调用 setDoInput。我会像这样重写你的代码:

url = new URL(vfmURLPrefix + vfmURL);
urlc = url.openConnection();
stream = new BufferedInputStream(urlc.getInputStream()); // connect/request will be made.

不过,我怀疑这能否解决您当前的问题。要调试您当前的问题,我建议您在浏览器中请求相同的图像 URL 时使用 Firebug 或 Chrome 的开发人员工具来嗅探网络连接/请求/响应。您可能会发现意外的重定向、状态代码、cookie 或类似内容。有了这些额外的知识,您就可以尝试将连接转换为 HttpURLConnection以便您有更多选项可用于自定义请求。

关于Java URL java.net.ConnectException 错误(Ping 有效,其他 URL 有效),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11777643/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com