I have a URL, http://www.skype.com/en/download-skype/skype-for-windows/downloading/. If I run this URL in Chrome the EXE file of Skype starts downloading. However if I write the code to download the file I am not able to do so. Here is my code:
public static void saveFile(URL url, String file) throws IOException {
System.out.println("opening connection");
InputStream in = url.openStream();
FileOutputStream fos = new FileOutputStream(new File(file));
System.out.println("Reading file...");
int length = -1;
byte[] buffer = new byte[1024]; // Buffer for portion of data from
// Connection
while ((length = in.read(buffer)) > -1) {
fos.write(buffer, 0, length);
}
fos.close();
in.close();
System.out.println("File was downloaded");
}
public static void main(String args[])
{
try
{
URL url = new URL("http://www.skype.com/en/download-skype/skype-for-windows/downloading/");
String fileName = "C:/SETUP/skype.exe";
saveFile(url, fileName);
}
catch(IOException e)
{
e.printStackTrace();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…