Skip to content

Commit ce02cd3

Browse files
authored
Create DownloadImage.java
1 parent 0857058 commit ce02cd3

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

arrays/DownloadImage.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.zetcode;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
7+
public class DownloadImage {
8+
9+
public static void main(String[] args) throws IOException {
10+
11+
var imageUrl = "http://webcode.me/favicon.ico";
12+
var destinationFile = "favicon.ico";
13+
14+
var url = new URL(imageUrl);
15+
16+
try (var is = url.openStream();
17+
var fos = new FileOutputStream(destinationFile)) {
18+
19+
byte[] b = new byte[1024];
20+
int noOfBytes;
21+
22+
while ((noOfBytes = is.read(b)) != -1) {
23+
24+
fos.write(b, 0, noOfBytes);
25+
}
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)