JAVA 通過url下載圖片保存到本地


 //java 通過url下載圖片保存到本地
    public static void download(String urlString, int i) throws Exception {
        // 構造URL
        URL url = new URL(urlString);
        // 打開連接
        URLConnection con = url.openConnection();
        // 輸入流
        InputStream is = con.getInputStream();
        // 1K的數據緩沖
        byte[] bs = new byte[1024];
        // 讀取到的數據長度
        int len;
        // 輸出的文件流
        String filename = "D:\\圖片下載/" + i + ".jpg";  //下載路徑及下載圖片名稱
        File file = new File(filename);
        FileOutputStream os = new FileOutputStream(file, true);
        // 開始讀取
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        System.out.println(i);
        // 完畢,關閉所有鏈接
        os.close();
        is.close();
    }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM