bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

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如何下載網絡文件
本文路徑:http://vcdvsql.cn/article40/iijoeo.html

成都網站建設公司_創新互聯,為您提供App開發動態網站域名注冊網站營銷電子商務定制網站

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站優化排名