1. 程式人生 > 其它 >java實現獲取url中的圖片儲存到本地

java實現獲取url中的圖片儲存到本地

技術標籤:學習筆記

實現背景

由於原本OSS伺服器即將過期,需要將存在資料庫裡的oss_url中的地址取出,訪問該地址並儲存圖片

程式碼實現

核心下載程式碼

private void download(List<String> listImgSrc) {
        try {
            for (String url : listImgSrc) {
                String imageName = url.substring(url.lastIndexOf("/") + 1, url.length());
                URL uri =
new URL(url); InputStream in = uri.openStream(); FileOutputStream fo = new FileOutputStream(new File("D:\\Personal\\Desktop\\goods\\"+imageName)); byte[] buf = new byte[1024]; int length = 0; System.out.println(
"開始下載:" + url); while ((length = in.read(buf, 0, buf.length)) != -1) { fo.write(buf, 0, length); } in.close(); fo.close(); System.out.println(imageName + "下載完成"); } }
catch (Exception e) { System.out.println("下載失敗"); } }

結合讀取資料庫完整程式碼:

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Cheertan
 * @date 2021-01-20
 */
public class CatchImage {
    // 地址
    private static final String URL = "http://url";
    // 編碼
    private static final String ECODING = "UTF-8";
    // 獲取img標籤正則
    private static final String IMGURL_REG = "<img.*src=(.*?)[^>]*?>";
    // 獲取src路徑的正則
    private static final String IMGSRC_REG = "http:\"?(.*?)(\"|>|\\s+)";


    public static void main(String[] args) throws Exception {

        List<String> imageUrl = new ArrayList<>();
        try {
            //呼叫Class.forName()方法載入驅動程式
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("成功載入MySQL驅動!");

            String url = "jdbc:mysql://localhost/litemall?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
            Connection conn;
            conn = DriverManager.getConnection(url, "root", "balabala");
            Statement stmt = conn.createStatement();
            System.out.println("成功連線到資料庫!");

            String sql = "select pic_url from litemall_goods";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
            //這裡getString後的數字代表篩選出的結果所在的列數,從1開始計數
                imageUrl.add(rs.getString(1));
                System.out.println();
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

                CatchImage cm = new CatchImage();
        //下載圖片
        cm.download(imageUrl);
    }




    private void download(List<String> listImgSrc) {
        try {
            for (String url : listImgSrc) {
                String imageName = url.substring(url.lastIndexOf("/") + 1, url.length());
                URL uri = new URL(url);
                InputStream in = uri.openStream();
                FileOutputStream fo = new FileOutputStream(new File("D:\\Personal\\Desktop\\goods\\"+imageName));
                byte[] buf = new byte[1024];
                int length = 0;
                System.out.println("開始下載:" + url);
                while ((length = in.read(buf, 0, buf.length)) != -1) {
                    fo.write(buf, 0, length);
                }
                in.close();
                fo.close();
                System.out.println(imageName + "下載完成");
            }
        } catch (Exception e) {
            System.out.println("下載失敗");
        }
    }
}

JAVA抓取網頁圖片