• 技术文章 >Java >Java入门

    java下载网络文件的方法有哪些

    王林王林2020-11-26 15:47:13转载1749

    下载网络文件的方法有:

    学习视频分享: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下载网络文件的方法有哪些的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:csdn,如有侵犯,请联系admin@php.cn删除
    专题推荐:java 网络文件
    上一篇:java中字节流与字符流的区别是什么 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • 用php实现远程网络文件下载到服务器• 使用PHPEXCEL读取网络文件• 使用PHP获取网络文件的实现代码_PHP教程• PHP判断网络文件是否存在
    1/1

    PHP中文网