我的连接超时了:连接异常在java代码中,请参阅下面。我在谷歌搜索过,但没有多少帮助,你可以在你的机器上运行这段代码,它的完整代码我在下面给出。密码-
public class download {
// final static int size=1024;
public static void downloadValuationPDFReport() {
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
String fAddress = null;
URL Url = null;
String localFileName = "abc.zip";
String destinationDir = "H:\\";//"C:\\Users\\501301605\\Downloads";
try {
fAddress = "http://www.novell.com/coolsolutions/tools/downloads/ntradping.zip";
byte[] buf;
int byteRead = 0;
Url = new URL(fAddress);
outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "\\" + localFileName));
uCon = Url.openConnection();
is = uCon.getInputStream();
buf = new byte[1024];
while ((byteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, byteRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}发布于 2015-05-27 09:39:01
很可能您在一个网络中,您不允许直接连接到任何端口80;尝试并:
telnet www.novell.com 80看看你是否得到了答案,这可能也会导致超时。
您更有可能需要使用代理(例如,请参阅here )。而且,您的代码留下了许多资源,您使用的是过时的File。
下面是如何在现代代码中这样做:
final Path dstfile = Paths.get("h:", "abc.zip");
// ...
try (
final InputStream in = url.openStream();
) {
Files.copy(in, dstfile, StandardOpenOption.CREATE_NEW);
}https://stackoverflow.com/questions/30478272
复制相似问题