Java URL Download downNetImg(String filePath, String remotePath, String htmlUrl, String fileName)

Here you can find the source of downNetImg(String filePath, String remotePath, String htmlUrl, String fileName)

Description

down Net Img

License

Apache License

Declaration

public static String downNetImg(String filePath, String remotePath, String htmlUrl, String fileName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static String downNetImg(String filePath, String remotePath, String htmlUrl, String fileName) {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();//from   w  w  w .j  av  a2  s .c o m
        }
        URL url = null;
        OutputStream os = null;
        try {
            url = new URL(htmlUrl);
            URLConnection con = url.openConnection();
            int lenss = con.getContentLength();
            InputStream is = url.openStream();
            os = new FileOutputStream(filePath + fileName);
            int length = 0;
            byte[] buffer = new byte[5 * 1024];
            int len = 0;
            while ((length = is.read(buffer, 0, 5 * 1024)) != -1) {
                len = length + len;
                double percent = Math.round((len / (double) lenss) * 100D);
                os.write(buffer, 0, length);
            }
            return remotePath + fileName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. downloadUrlToFile(URL url, File file)
  2. downloadUrlToFile(URL url, File result)
  3. downloadWebpage(String url)
  4. downloadZip(URL file, File dump)
  5. downlod(String url, File dest)
  6. downPicture(String filePath, String imageUrl, String fileName)
  7. fetchUrl(String _url, String charset)
  8. fetchURL(String url)
  9. fetchURL(String url_string)