-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySaver.java
63 lines (60 loc) · 2.37 KB
/
BinarySaver.java
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
54
55
56
57
58
59
60
61
62
63
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class BinarySaver {
public static void main(String[] args) {
System.out.println("Enter URL:");
Scanner input = new Scanner(System.in);
String myurl = input.nextLine();
input.close();
try {
URL root = new URL(myurl);
saveBinaryFile(root);
} catch (MalformedURLException ex) {
System.err.println(myurl + " is not a URL I understand.");
} catch (IOException ex) {
System.err.println("Error: " + ex.getMessage());
}
}
public static void saveBinaryFile(URL u) throws IOException {
URLConnection uc = u.openConnection();
uc.setRequestProperty("User-Agent", "Mozilla/5.0");
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
if (contentType == null || contentType.startsWith("text/") || contentLength == -1) {
throw new IOException("This is not a binary file or the content length is invalid.");
}
try (InputStream raw = uc.getInputStream()) {
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int offset = 0;
while (offset < contentLength) {
int bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1)
break;
offset += bytesRead;
}
if (offset != contentLength) {
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
}
String filename = getFileName(u);
try (FileOutputStream fout = new FileOutputStream(filename)) {
fout.write(data);
fout.flush();
System.out.println("File saved as " + filename);
}
}
}
private static String getFileName(URL u) {
String filename = u.getFile();
if (filename == null || filename.isEmpty() || filename.equals("/")) {
filename = "downloaded_file";
} else {
filename = filename.substring(filename.lastIndexOf('/') + 1);
if (filename.contains("?")) {
filename = filename.substring(0, filename.indexOf('?'));
}
}
return filename;
}
}