java如何下载网络文件

这篇文章主要介绍java如何下载网络文件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联公司制作网站网页找三站合一网站制作公司,专注于网页设计,成都网站设计、网站制作,网站设计,企业网站搭建,网站开发,建网站业务,680元做网站,已为数千家服务,创新互联公司网站建设将一如既往的为我们的客户提供最优质的网站建设、网络营销推广服务!

下载网络文件的方法有:

  • 字节流下载

  • apache的FileUtils工具包下载

  • NIO下载

学习视频分享:java教学视频

实现代码如下:

package com.dsp.rpc.metricelf;
 
import org.apache.commons.io.FileUtils;
 
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
 
 
public class DownloadZipUtil {
 
    /**
     * FileUtils下载网络文件
     *
     * @param serverUrl   :网络文件地址
     * @param savePath:本地保存路径
     * @param zipSavePath :压缩文件保存路径
     * @return
     */
    public static String downloadFile(String serverUrl, String savePath, String zipSavePath) throws Exception {
        String result;
        File f = new File(savePath);
        if (!f.exists()) {
            if (!f.mkdirs()) {
                throw new Exception("makdirs: '" + savePath + "'fail");
            }
        }
        URL url = new URL(serverUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(3 * 1000);
        //防止屏蔽程序抓取而放回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 5.0;Windows NT;DigExt)");
        Long totalSize = Long.parseLong(conn.getHeaderField("Content-Length"));
        if (totalSize > 0) {
            FileUtils.copyURLToFile(url, new File(zipSavePath));
            result = "success";
        } else {
            throw new Exception("can not find serverUrl :{}" + serverUrl);
        }
        return result;
    }
 
 /**
     * 字节流下载压缩文件
     * @param serverUrl :网络地址
     * @param savePath :保持路径
     * @param zipSavePath :压缩文件保持路径
     * @return :下载结果
     * @throws Exception :异常
     */
    public static String downloadZip(String serverUrl,String savePath,String zipSavePath) throws Exception{
        String result = "fail";
        File f = new File(savePath);
        if(!f.exists()){
            if (!f.mkdirs()) {
                throw new Exception("makdirs: '" + savePath + "'fail");
            }
        }
        //Sardine是WebDAV的工具包
        Sardine sardine = SardineFactory.begin("test","test");
        if(sardine.exists(serverUrl)){
            URL url = new URL(serverUrl);
            URLConnection conn = url.openConnection();
            int length = conn.getContentLength();
            conn.setConnectTimeout(3 * 1000);
            // 防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            InputStream is = sardine.getInputStream(serverUrl);
            BufferedInputStream bis = new BufferedInputStream(is);
            FileOutputStream fos = new FileOutputStream(zipSavePath);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            int len;
            byte[] bytes = new byte[length/5];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            //清除缓存
            bos.flush();
            //关闭流
            fos.close();
            is.close();
            bis.close();
            bos.close();
            result = "success";
 
        }else {
             throw new Exception("can not find file");
        }
        return result;
    }
 
 
 
}

以上是“java如何下载网络文件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!

网页名称:java如何下载网络文件
本文路径:https://www.cdcxhl.com/article40/iijoeo.html

成都网站建设公司_创新互联,为您提供App开发动态网站域名注册网站营销电子商务定制网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站优化排名