-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDownloadManager.java
More file actions
45 lines (35 loc) · 1.22 KB
/
DownloadManager.java
File metadata and controls
45 lines (35 loc) · 1.22 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
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownloadManager {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
final int EOF = -1;
String urlAddress = "http://3.bp.blogspot.com/-Cq29XEXLbuM/UNfubYyxnzI/AAAAAAAAAX8/faSgIbDAZkU/s1600/sachin-tendulkars.jpg";
URL url = new URL(urlAddress);
String basepath = "/Users/amit/Documents/TestFileHandlingFeb/";
int index = urlAddress.lastIndexOf("/");
String path = basepath + urlAddress.substring(index+1);
FileOutputStream fo = new FileOutputStream(path);
BufferedOutputStream bo = new BufferedOutputStream(fo);
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bi = new BufferedInputStream(is);
int singleByte = bi.read();
while(singleByte !=EOF){
//System.out.print((char)singleByte);
bo.write(singleByte);
singleByte = bi.read();
}
bo.close();
bi.close();
fo.close();
is.close();
System.out.println("File Store...");
}
}