国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Java批量下載書籍圖片并保存為PDF的方法

這篇具有很好參考價值的文章主要介紹了Java批量下載書籍圖片并保存為PDF的方法。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

背景

因為經(jīng)常出差火車上沒網(wǎng)、不方便電子書閱讀器批注,需要從某網(wǎng)站上批量下載多本書籍的圖片并自動打包成PDF文件。

分析

1、嘗試獲得圖片地址,發(fā)現(xiàn)F12被禁
解決方法:使用Chrome瀏覽器,點擊右上角三個點呼出菜單,選擇“更多工具”->“開發(fā)者工具”
或者使用Ctrl+Shift+C、Ctrl+Shift+I
2、審查元素,發(fā)現(xiàn)圖片地址非常有規(guī)律:
在class為side-image的div里有一個img,src是../files/mobile/1.jpg?220927153454,去掉后面的問號部分即可得到/files/mobile/1.jpg,通過觀察,這本書一共有多少頁就會有多少個.jpg文件
3、回到欄目頁,可得到基目錄,所以批量抓取的大致思路是從欄目頁獲得基目錄,然后不斷累加一個數(shù),直到獲得jpg時對方服務(wù)器報404錯誤,即可得到剛剛處理的那一頁即最后一頁。
4、如何從欄目頁獲得基目錄呢?
經(jīng)觀察,每個page_pc_btm_book_body里都有兩個a標(biāo)簽,第一個是圖片,第二個是“在線閱讀”按鈕,但是需要翻頁怎么辦呢?所以需要建立一個變量收集它們,每翻一頁,做一次收集。于是可以寫如下收集函數(shù):

let books=[]
function catchBook() {
  let links = document.getElementsByClassName("page_pc_btm_book_body");
  for (let i in links) {
    if(!links[i].children||links[i].children.length<2)continue;
    let title = links[i].children[0].title;
    let link = links[i].children[0].href;
    books.push({title,link})
  }
}

然后在瀏覽器里每翻一頁,在控制臺里執(zhí)行一次catchBook,這樣書名和基目錄就都獲得了。
5、如何把JSON導(dǎo)出來呢
在控制臺里JSON.stringify(books),把結(jié)果復(fù)制出來,然后到網(wǎng)上隨便找一個JSON轉(zhuǎn)Excel的工具,轉(zhuǎn)出來即可,然后注意把第一行當(dāng)表頭,數(shù)據(jù)復(fù)制到第二行開始。
6、最后一步就寫個程序從Excel里讀出數(shù)據(jù),把圖片都批量抓下來即可,下面就說說如何寫程序來處理。

需要引的包

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

從Excel到實體

先定義一個實體,這里我多加了一列type,表示類型,name就是從上面那個里面獲得的title,link就是上面獲得的link屬性。

import lombok.Data;

@Data
public class Book {
    private String type;
    private String name;
    private String link;
}

然后寫個ExcelReader

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExcelReader {

    public static List<Book> readXlsxToList(String filePath) {
        List<Book> bookList = new ArrayList<>();

        try (FileInputStream fileInputStream = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fileInputStream)) {

            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();

            // 獲取表頭(第一行)并轉(zhuǎn)換為屬性數(shù)組
            Row headerRow = rowIterator.next();
            String[] headers = getRowDataAsStringArray(headerRow);

            // 遍歷每一行(從第二行開始)
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Book book = new Book();

                // 遍歷每個單元格,并根據(jù)屬性名稱設(shè)置對應(yīng)的實體類屬性值
                for (Cell cell : row) {
                    int columnIndex = cell.getColumnIndex();
                    if (columnIndex < headers.length) {
                        String headerValue = headers[columnIndex];
                        String cellValue = getCellValueAsString(cell);

                        setBookProperty(book, headerValue, cellValue);
                    }
                }

                bookList.add(book);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return bookList;
    }

    private static String[] getRowDataAsStringArray(Row row) {
        String[] rowData = new String[row.getLastCellNum()];
        for (Cell cell : row) {
            int columnIndex = cell.getColumnIndex();
            rowData[columnIndex] = getCellValueAsString(cell);
        }
        return rowData;
    }

    private static String getCellValueAsString(Cell cell) {
        String cellValue = "";
        if (cell != null) {
            switch (cell.getCellType()) {
                case STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                case NUMERIC:
                    cellValue = String.valueOf(cell.getNumericCellValue());
                    break;
                case BOOLEAN:
                    cellValue = String.valueOf(cell.getBooleanCellValue());
                    break;
                case FORMULA:
                    cellValue = cell.getCellFormula();
                    break;
                default:
                    cellValue = "";
            }
        }
        return cellValue;
    }

    private static void setBookProperty(Book book, String propertyName, String propertyValue) {
        switch (propertyName) {
            case "type":
                book.setType(propertyValue);
                break;
            case "name":
                book.setName(propertyValue);
                break;
            case "link":
                book.setLink(propertyValue);
                break;
            // 添加其他屬性
            default:
                // 未知屬性,可以根據(jù)需要進行處理
                break;
        }
    }
}

從實體集合到批量下載成jpg

還需要想辦法實現(xiàn)批量下載的功能,需要注意的是Windows的默認(rèn)文件排序是按ASC碼排序的,會把10.jpg排在2.jpg前面,所以需要對頁碼格式化一下,把它變成三位數(shù)。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class ImageDownloader {

    public static void downloadImages(List<Book> bookList, String targetDir) {
        for (Book book : bookList) {
            String type = book.getType();
            String name = book.getName();
            String link = book.getLink();

            String basePath = targetDir + "/" + type + "/" + name;

            int count = 1;
            boolean continueDownload = true;

            if(!new File(basePath).exists()){
                new File(basePath).mkdirs();
            }
            while (continueDownload) {
                String imgUrl = link + "files/mobile/" + count + ".jpg";
                String outputPath = String.format("%s/%03d.jpg", basePath, count);

                if (!imageExists(outputPath)) {
                    try {
                        downloadImage(imgUrl, outputPath);
                        System.out.println("Downloaded: " + outputPath);
                    } catch (IOException e) {
                        System.out.println("Error downloading image: " + imgUrl);
                        e.printStackTrace();
                        continueDownload = false;
                    }
                } else {
                    System.out.println("Image already exists: " + outputPath);
                }

                count++;
            }
        }
    }

    private static boolean imageExists(String path) {
        Path imagePath = Paths.get(path);
        return Files.exists(imagePath);
    }

    private static void downloadImage(String imageUrl, String outputPath) throws IOException {
        URL url = new URL(imageUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        int responseCode = httpConn.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            try (InputStream inputStream = httpConn.getInputStream();
                 FileOutputStream outputStream = new FileOutputStream(outputPath)) {

                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            }
        } else {
            throw new IOException("Server returned response code " + responseCode);
        }
    }
}

開始批量下載

import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Book> books = ExcelReader.readXlsxToList("C:\\Users\\Administrator\\Desktop\\某某書庫.xlsx");
        String targetDir = "D:\\書庫\\";
        ImageDownloader.downloadImages(books, targetDir);
    }
}

寫完執(zhí)行,回去睡一覺

jpg圖片批量轉(zhuǎn)成pdf

都下載完之后,就可以想辦法批量轉(zhuǎn)成PDF格式了。

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

public class ImageToPdfConverter {
    public static void convertToPdf(String folderPath, String outputFilePath) {
        try {
            // 獲取文件夾中的所有jpg文件
            File folder = new File(folderPath);
            File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".jpg"));

            // 預(yù)讀第一章圖片獲得大小
            Rectangle rect = null;
            if (files.length == 0) {
                return;
            } else {
                Image image = Image.getInstance(files[0].getAbsolutePath());
                rect = new Rectangle(image.getWidth(), image.getHeight());
            }

            // 創(chuàng)建PDF文檔對象
            Document document = new Document(rect);
            document.setMargins(0, 0, 0, 0);
            // 創(chuàng)建PDF寫入器
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFilePath));
            writer.setStrictImageSequence(true);
            // 打開PDF文檔
            document.open();

            // 遍歷圖片文件并將其加入到PDF文檔中
            for (File file : files) {
                Image image = Image.getInstance(file.getAbsolutePath());
                document.add(image);
            }

            // 關(guān)閉PDF文檔
            document.close();

            System.out.println("PDF文件生成成功!");
        } catch (FileNotFoundException | DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String startDir="D:\\書庫\\開發(fā)技術(shù)\\";
        File[] subdirs = new File(startDir).listFiles();
        for (File subdir : subdirs) {
            if(subdir.isDirectory()){
                convertToPdf(subdir.getAbsolutePath(), subdir.getAbsolutePath()+".pdf");
            }
        }
    }
}

結(jié)束

最后把PDF文件傳到網(wǎng)盤上,手機、平板、電腦隨時可以下載離線看,非常舒服。

注意:自己抓取書籍自己看無所謂,但通過網(wǎng)絡(luò)分享出去是侵犯他人著作權(quán)的。文章來源地址http://www.zghlxwxcb.cn/news/detail-645479.html

到了這里,關(guān)于Java批量下載書籍圖片并保存為PDF的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Java Word轉(zhuǎn)PDF(直接轉(zhuǎn)和以圖片形式轉(zhuǎn))、PDF轉(zhuǎn)圖片、圖片轉(zhuǎn)PDF

    在淘寶上找了一家寫代碼的店鋪寫了一個工具類,再參考網(wǎng)上的代碼,改了改 用到的類庫: 工具類代碼:

    2024年02月12日
    瀏覽(26)
  • Java pdf轉(zhuǎn)圖片

    2024年02月12日
    瀏覽(12)
  • 【生成PDF】【JAVA】純后臺生成Echarts圖片,并將圖片生成到PDF文檔

    【生成PDF】【JAVA】純后臺生成Echarts圖片,并將圖片生成到PDF文檔

    目錄 前言 一、如何后臺生成Echarts圖片? 1.PhantomJS 2.PhantomJS的下載 ?3.用phantomjs調(diào)用echarts-converts.js生成圖片 二、Java如何將Echarts圖生成到PDF 1.生成PDF依賴 2.Java代碼測試?yán)樱??3.測試結(jié)果? ?三、下載生成的PDF ReportFormUtil 提示:本文僅用于記錄日常,多有不足,僅供參考。

    2024年02月09日
    瀏覽(32)
  • JAVA 實現(xiàn)PDF轉(zhuǎn)圖片(spire.pdf.free版)

    JAVA 實現(xiàn)PDF轉(zhuǎn)圖片(spire.pdf.free版)

    1. 引入 jar 包 導(dǎo)入方法1: 手動引入。將Free Spire.PDF for Java下載到本地,解壓,找到lib文件夾下的Spire.PDF.jar文件。在IDEA中打開如下界面,將本地路徑中的jar文件引入Java程序: ?導(dǎo)入方法2:如果您想通過?Maven安裝,則可以在 pom.xml 文件中添加以下代碼導(dǎo)入 JAR 文件。 1 2 3 4 5

    2024年02月05日
    瀏覽(29)
  • Java實現(xiàn)圖片轉(zhuǎn)PDF

    某w*s圖片轉(zhuǎn)PDF還要收費,簡直不講武德!我啪的一下,很快啊,一段代碼搞定! 引入pom依賴 工具類

    2024年01月15日
    瀏覽(27)
  • java中預(yù)覽pdf或者圖片

    java中預(yù)覽pdf或者圖片

    ? ?

    2024年02月12日
    瀏覽(20)
  • JAVA 實現(xiàn)PDF轉(zhuǎn)圖片格式

    JAVA 實現(xiàn)PDF轉(zhuǎn)圖片格式

    依賴: pdf存放路徑 正文開始: pdf轉(zhuǎn)換多張圖片、長圖 展示效果: 附加:小程序預(yù)覽wxml代碼

    2024年02月06日
    瀏覽(16)
  • pdf轉(zhuǎn)圖片【java版實現(xiàn)】

    引入需要導(dǎo)入到項目中的依賴,如下所示: pdf轉(zhuǎn)圖片的工具類如下所示,直接拷貝到項目即可 執(zhí)行工具類中的main方法就行,會將pdf文件轉(zhuǎn)換成多張圖片到同級目錄中。

    2024年02月13日
    瀏覽(21)
  • JAVA PDF 給PDF添加文字/圖片水?。ㄖ付▋?nèi)容),并且設(shè)置位置

    JAVA PDF 給PDF添加文字/圖片水?。ㄖ付▋?nèi)容),并且設(shè)置位置

    提示:看完這個簡單的demo 后就知道怎樣去操作一個PDF了 文章目錄 前言 一、前提準(zhǔn)備 二、使用步驟 1.引入庫 2.以下是部分代碼的作用 總結(jié) 提示:操作PDF其實是一件很簡單的事情,比一般的CRUD都簡單 例如:我們拿到了一個需求,我需要給這個PDF設(shè)置一個 電子簽名 ( 就是一

    2024年04月23日
    瀏覽(29)
  • 批量打印-----jsPDF將圖片轉(zhuǎn)為pdf,并合并pdf

    安裝依賴并引入 注意一、 使用jspdf將圖片(jpg/jpeg/png/bmp)轉(zhuǎn)pdf(記為pdfA),得到的pdf(pdfA)和需要合并的pdf(記為pdfB)類型不一致,需要將pdfA轉(zhuǎn)為pdfB類型,才能合并,使用arraybuffer轉(zhuǎn),具體如下 注意二、 jspdf 可轉(zhuǎn)pdf的圖片類型有jpg、jpeg、png、bpm, 不支持 tif 和 tiff 圖片類

    2024年02月13日
    瀏覽(17)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包