Java 下载图片下载文件 工具类

//                         _ooOoo_  
//                        o8888888o  
//                        88" . "88  
//                        (| -_- |)  
//                         O\ = /O  
//                     ____/`---'\____  
//                   .   ' \\| |// `.  
//                    / \\||| : |||// \  
//                  / _||||| -:- |||||- \  
//                    | | \\\ - /// | |  
//                  | \_| ''\---/'' | |  
//                   \ .-\__ `-` ___/-. /  
//                ___`. .' /--.--\ `. . __  
//             ."" '< `.___\_<|>_/___.' >'"".  
//            | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//              \ \ `-. \_ __\ /__ _/ .-` / /  
//      ======`-.____`-.___\_____/___.-`____.-'======  
//                         `=---='  
//
//      .............................................  
//               佛祖保佑             永无BUG 
//       佛曰:  
//               写字楼里写字间,写字间里程序员;  
//               程序人员写程序,又拿程序换酒钱。  
//               酒醒只在网上坐,酒醉还来网下眠;  
//               酒醉酒醒日复日,网上网下年复年。  
//               但愿老死电脑间,不愿鞠躬老板前;  
//               奔驰宝马贵者趣,公交自行程序员。  
//               别人笑我忒疯癫,我笑自己命太贱;  
//               不见满街漂亮妹,哪个归得程序员? 

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;

/**
 * 下载 工具类
 * 
 * @author sun
 */
public class DownloadUtils {
	
	/**
	 * 下载文件到本地
	 * @author sun
	 * @date 2018年3月25日 上午11:01:05
	 * @param urlString
	 * @param filename
	 * @throws Exception
	 */
	public static void download(String urlString, String filename)
			throws Exception {
		URL url = new URL(urlString);// 构造URL
		URLConnection con = url.openConnection();// 打开连接
		InputStream is = con.getInputStream();// 输入流
		String code = con.getHeaderField("Content-Encoding");
		if ((null != code) && code.equals("gzip")) {
			GZIPInputStream gis = new GZIPInputStream(is);
			// 1K的数据缓冲
			byte[] bs = new byte[1024];
			// 读取到的数据长度
			int len;
			// 输出的文件流
			OutputStream os = new FileOutputStream(filename);
			// 开始读取
			while ((len = gis.read(bs)) != -1) {
				os.write(bs, 0, len);
			}
			// 完毕,关闭所有链接
			gis.close();
			os.close();
			is.close();
		} else {
			// 1K的数据缓冲
			byte[] bs = new byte[1024];
			// 读取到的数据长度
			int len;
			// 输出的文件流
			OutputStream os = new FileOutputStream(filename);
			// 开始读取
			while ((len = is.read(bs)) != -1) {
				os.write(bs, 0, len);
			}
			// 完毕,关闭所有链接
			os.close();
			is.close();
		}
	}
	
	public static void main(String[] args) {
		try {
			download("http://www.7-zip.org/a/7z1604.exe", "/Users/sun/Documents/a.exe");
			download("https://www.baidu.com/img/bd_logo1.png", "/Users/sun/Documents/b.png");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
版权属于: 技术客
原文地址: https://www.sunjs.com/article/detail/ca69445015e44ffa9d6ef78abc000efc.html
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值