I am getting Connection timed out: connect exception in java code please see below. I have searched in google but haven't got much help,U can run this code in your machine,its complete code I am giving below. code-
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();
}
}
}
}
It is very likely that you are within a network where you are not allowed to connect directly to anything port 80; try and:
telnet www.novell.com 80
and see if you get an answer; this will probably result in a timeout as well.
More likely than not you need to use a proxy (see here for instance). Also, your code leaves many resources dangling, and you use File
which is obsolete.
Here is how to do it in modern code:
final Path dstfile = Paths.get("h:", "abc.zip");
// ...
try (
final InputStream in = url.openStream();
) {
Files.copy(in, dstfile, StandardOpenOption.CREATE_NEW);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.