How to Download a Binary File using Java? - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

How to Download a Binary File using Java?

Share This
BinaryFileDownloader.java
import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class BinaryFileDownloader { public static void main(String args[]) { for (int i = 0; i < args.length; i++) { try { URL root = new URL(args[i]); saveBinaryFile(root); } catch (MalformedURLException e) { System.err.println(args[i] + " is not URL I understand."); } catch (IOException e) { System.err.println(e); } } } public static void saveBinaryFile(URL u) throws IOException { URLConnection uc = u.openConnection(); String contentType = uc.getContentType(); int contentLength = uc.getContentLength(); if (contentType.startsWith("text/") || contentLength == -1) { throw new IOException("This is not a binary file."); } InputStream raw = uc.getInputStream(); InputStream in = new BufferedInputStream(raw); byte[] data = new byte[contentLength]; int bytesRead = 0; int offset = 0; while (offset < contentLength) { bytesRead = in.read(data, offset, data.length - offset); if (bytesRead == -1) break; offset += bytesRead; } in.close(); if (offset != contentLength) { throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); } String filename = u.getFile(); filename = filename.substring(filename.lastIndexOf('/') + 1); FileOutputStream fout = new FileOutputStream(filename); fout.write(data); fout.flush(); fout.close(); } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.