Java Utililty Methods URL Download

List of utility methods to do URL Download

Description

The list of methods to do URL Download are organized into topic(s).

Method

voiddownloadUrlToFile(String surl, File file, String method)
download Url To File
URL url = new URL(surl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (notEmpty(method))
    conn.setRequestMethod(method);
else
    conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
...
voiddownloadUrlToFile(URL url, File file)
Downloads the byte contents of a URL to a specified file.
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
    is = new BufferedInputStream(url.openStream());
    os = new BufferedOutputStream(new FileOutputStream(file));
    byte[] b = new byte[4096];
    int len = -1;
    while ((len = is.read(b)) != -1)
...
voiddownloadUrlToFile(URL url, File result)
download Url To File
IOException exception = null;
InputStream is = null;
DataInputStream dis = null;
FileOutputStream fos = null;
byte[] buf = new byte[1024];
try {
    is = url.openStream();
    dis = new DataInputStream(new BufferedInputStream(is));
...
StringdownloadWebpage(String url)
download Webpage
try {
    return downloadWebpage(new URL(url));
} catch (MalformedURLException e) {
    return null;
voiddownloadZip(URL file, File dump)
Downloads a Zip Archive and extracts it to specified folder.
ZipInputStream in = new ZipInputStream(file.openStream());
ZipEntry en = in.getNextEntry();
while (en != null) {
    recursiveUnpack(in, en, dump);
    en = in.getNextEntry();
in.close();
voiddownlod(String url, File dest)
downlod
int retry = 0;
while (++retry <= 3) {
    System.out.println("Downloading " + url + " Retry count: " + retry);
    try {
        URLConnection conn = new URL(url).openConnection();
        conn.setConnectTimeout(TIMEOUT);
        conn.setReadTimeout(TIMEOUT);
        InputStream in = conn.getInputStream();
...
StringdownNetImg(String filePath, String remotePath, String htmlUrl, String fileName)
down Net Img
File file = new File(filePath);
if (!file.exists()) {
    file.mkdirs();
URL url = null;
OutputStream os = null;
try {
    url = new URL(htmlUrl);
...
MapdownPicture(String filePath, String imageUrl, String fileName)
down Picture
Map<String, Object> map = new HashMap<String, Object>();
Integer size = null;
String mime = null;
try {
    File files = new File(filePath);
    if (!files.exists()) {
        files.mkdirs();
    URL url = new URL(imageUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    InputStream is = connection.getInputStream();
    if (!filePath.endsWith("/")) {
        filePath = filePath + "/";
    File file = new File(filePath + fileName);
    FileOutputStream out = new FileOutputStream(file);
    int i = 0;
    while ((i = is.read()) != -1) {
        out.write(i);
    size = connection.getContentLength();
    mime = connection.getContentType();
    is.close();
} catch (Exception e) {
map.put("size", size);
map.put("mime", mime);
return map;
StringfetchUrl(String _url, String charset)
fetch Url
BufferedReader reader = null;
try {
    URL url = new URL(_url);
    if (charset == null)
        reader = new BufferedReader(new InputStreamReader(url.openStream()));
    else
        reader = new BufferedReader(new InputStreamReader(url.openStream(), charset));
    StringBuilder sb = new StringBuilder();
...
StringfetchURL(String url)
fetch URL
String content = null;
URLConnection connection = null;
try {
    connection = new URL(url).openConnection();
    Scanner scanner = new Scanner(connection.getInputStream());
    scanner.useDelimiter("\\Z");
    content = scanner.next();
    return content;
...