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

发布时间:2018-03-27作者:laosun阅读(5161)

Java

Java 下载图片下载文件 工具类,下载gzip压缩文件

    //                         _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();
            }
        }
    
    }


3 +1

版权声明

 Java  源码

 请文明留言

0 条评论