搜尋
首頁JavaJava入門java下載網頁檔案的方法有哪些

java下載網頁檔案的方法有哪些

下載網路檔案的方法有:

  • #位元組流下載

  • 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下載網頁檔案的方法有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:csdn。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能