-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProto.java
More file actions
45 lines (31 loc) · 1.64 KB
/
Proto.java
File metadata and controls
45 lines (31 loc) · 1.64 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
import java.io.*;
import java.net.*;
public class Proto {
public static void main(String[] args){
final String link = "https://www.w3schools.com/images/myw3schoolsimage.jpg"; // link to image
try{
URL url = new URL(link); // Create an URL object
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Open a connection to the URL
for(int i=1;i<=20;i++){
if(conn.getHeaderFieldKey(i)==null) break;
System.out.println(conn.getHeaderFieldKey(i)+" = "+conn.getHeaderField(i));
}
int fieldValue = conn.getContentLength();// Get the content disposition field value
System.out.println(fieldValue);
System.out.println("Content-Disposition: " + conn.getHeaderField("Content-Type"));
System.out.println(MimeTypes.lookupExt(conn.getHeaderField("Content-Type")));
// Write the content to a file
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
String fileName = "download." + MimeTypes.lookupExt(conn.getHeaderField("Content-Type")); // Create a file name
FileOutputStream fos = new FileOutputStream(fileName); // Create a file output stream
byte[] buffer = new byte[1024]; // Create a buffer
int len; // Length of the buffer
while((len = in.read(buffer)) != -1){ // Read the content
fos.write(buffer, 0, len); // Write the buffer to the file
}
}
catch(Exception e){
System.out.println("Error: " + e);
}
}
}