Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class Main { private static String AGENT = "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:17.0) Gecko/17.0 Firefox/17."; public static boolean download(String uri, String filePath) { try { URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("User-Agent", AGENT); readStreamToFile(conn.getInputStream(), filePath); } catch (Exception e) { e.printStackTrace(); return false; } return true; } public static void readStreamToFile(InputStream inStream, String filePath) throws Exception { File file = new File(filePath + ".wei"); RandomAccessFile outStream = new RandomAccessFile(file, "rw"); outStream.seek(0); byte[] buffer = new byte[1024]; int len = -1; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } outStream.close(); inStream.close(); file.renameTo(new File(filePath)); return; } }