Thursday, June 19, 2014

JAVA: How to download file from internet - options.

There are several methods, but here few which I use or used:
1) Old-school, quite simple but less effective of all. 
            URL url = new URL(address);
            OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
            URLConnection conn = url.openConnection();
            InputStream in = conn.getInputStream();
            byte[] buffer = new byte[1024];

            int numRead;
            long numWritten = 0;

            while ((numRead = != -1) {
                out.write(buffer, 0, numRead);
                numWritten += numRead;

2) Use apache commons-io, just one line code:, File)

3)  Using Apache's HttpClient - where you can manipulate different types of requests method (setting get and post parameters  - Here is a great tutorial on using HttpClient

4) Give a try to Java NIO (again introduced in 1.4 - still almost no one is using nio!)
URL website = new URL(address);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(fileName);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them. Long.MAX_VALUE will allow at most 2^63 bytes (larger than any file in existence).
Check more about it here.

Web 3 - blockchain layers

Layers from a blockchain perspective. My plan is to write 5 articles:  1 Intro: Web 1.. 2.. 3.. 2 Layers in crypto.  [this one] 3 Applicatio...