forked from yfain/Java4Kids_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDownload.java
More file actions
53 lines (40 loc) · 1.44 KB
/
Copy pathFileDownload.java
File metadata and controls
53 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
class FileDownload{
public static void main(String args[]){
if (args.length!=2){
System.out.println(
"Proper Usage: java FileDownload FileURL DestinationFileName");
System.out.println(
"For example: java FileDownload http://myflex.org/yf/nyc.jpg nyc.jpg");
System.exit(-1);
}
URLConnection fileStream=null;
try{
URL remoteFile=new URL(args[0]);
fileStream=remoteFile.openConnection();
} catch (IOException ioe){
ioe.printStackTrace();
}
Path path = Paths.get(args[1]);
try(OutputStream fOut= Files.newOutputStream(path);
InputStream in = fileStream.getInputStream();){
System.out.println("Downloading from " + args[0] + ". Please wait...");
// Read a remote file and save it in the local one
int data;
while((data=in.read())!=-1){
fOut.write(data);
}
System.out.println("Finished downloading the file. It's located at "+path.toAbsolutePath());
} catch (Exception e){
e.printStackTrace();
}
}
}