Here you can find the source of downNetImg(String filePath, String remotePath, String htmlUrl, String fileName)
public static String downNetImg(String filePath, String remotePath, String htmlUrl, String fileName)
//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(); } } } }