Donwloading file from website automatically in java.

Imagine you want to download a file from a website in a binary format, where you have account [username, password]. Here is a java snippet that would allow you to do that.
In this case the snippet would specifically allow you to download a file in binary not in a text format.

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class WebDownloader {

public static void main(String[] arg){
try {
// Construct data
File file = new File(“file/path/for/the/file/to/be/downloaded”);
FileOutputStream fos = new FileOutputStream(file);
String data = URLEncoder.encode(“”, “UTF-8”) + “=” + URLEncoder.encode(“ur”, “UTF-8”);
data += “&” + URLEncoder.encode(“”, “UTF-8”) + “=” + URLEncoder.encode(“”, “UTF-8”);
// Send data
URL url = new URL(“your/url/goes/here”);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data); wr.flush();
BufferedInputStream rd = new BufferedInputStream(conn.getInputStream());
int count;
byte[] byt = new byte[256];
do{
count = rd.read(byt);
if(count != -1)
fos.write(byt, 0, count);
}while(true);
wr.close();
rd.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println(“downloaded”);
}
}

The above snippet is a very slight touch of the code snippet from exampledepot –