word 曙咽,pdf 省店,轉(zhuǎn)圖片通過base 64 轉(zhuǎn)前端

@PostMapping("/convertToBase64/{fileRename}")
public AjaxResult convertToBase64(@PathVariable String fileRename) throws IOException {
List<String> base64Images = new ArrayList<>();
if (StringUtils.isEmpty(fileRename))
return AjaxResult.error("文件不能為空");
String result = businessManagementService.detailsNodeDataDownload(fileRename);
if (StringUtils.isEmpty(result) || result.contains("access")){
return AjaxResult.error("下載失敻筒稹宙拉!");
}

    String fileUrl = result;
    int dotIndex = fileRename.lastIndexOf('.');
    String fileType =   fileRename.substring(dotIndex + 1);

    byte[] fileBytes;
    if ("pdf".equalsIgnoreCase(fileType)) {
        // 對(duì)PDF文件進(jìn)行轉(zhuǎn)換
        base64Images = convertPdfToBase64(fileUrl);
    } else if ("img".equalsIgnoreCase(fileType)||"png".equalsIgnoreCase(fileType)) {
        // 對(duì)圖片文件進(jìn)行轉(zhuǎn)換
        fileBytes = convertImageToBase64(fileUrl);
        String base64File = "data:image/png;base64," + Base64.getEncoder().encodeToString(fileBytes);
        base64Images.add(base64File);

    } else if ("doc".equalsIgnoreCase(fileType)||"docx".equals(fileType)) {
        // 對(duì)Word文件進(jìn)行轉(zhuǎn)換
        base64Images = convertWordToBase64(fileUrl);

    } else {
        return error("Unsupported file type");
    }


    return success(base64Images);
}

private List<String> convertPdfToBase64(String imageUrl) throws IOException {
    // 從遠(yuǎn)程 URL 獲取 PDF 文檔
    URL url = new URL(imageUrl);
    try (PDDocument document = PDDocument.load(url.openStream())) {
        int numPages = document.getNumberOfPages();
        List<String> base64Images = new ArrayList<>();
        // 使用 PDFBox 將 PDF 轉(zhuǎn)換為 BufferedImage
        PDFRenderer renderer = new PDFRenderer(document);
        for (int pageIndex = 0; pageIndex < numPages; pageIndex++) {
            BufferedImage image = renderer.renderImage(pageIndex);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", baos);

            byte[] imageBytes = baos.toByteArray();
            String base64Image = "data:image/png;base64," + Base64.getEncoder().encodeToString(imageBytes);

            base64Images.add(base64Image);
        }
        return base64Images;
    }
}

private byte[] convertImageToBase64(String imageUrl) throws IOException {
    URL url = new URL(imageUrl);
    BufferedImage image = ImageIO.read(url);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);

    return baos.toByteArray();
}

private List<String> convertWordToBase64(String wordFilePath) throws IOException {
    // 創(chuàng)建Document實(shí)例
    Document doc = new Document();
    List<String> base64Images = new ArrayList<>();
    InputStream inputStream = new URL(wordFilePath).openStream();
    // 加載Word文檔
    doc.loadFromStream(inputStream, FileFormat.Auto);

    BufferedImage[] images = doc.saveToImages(0, doc.getPageCount(), ImageType.Bitmap, 500, 500);

    for (BufferedImage image : images) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        String base64Image = "data:image/png;base64," + Base64.getEncoder().encodeToString(baos.toByteArray());

        base64Images.add(base64Image);
     //   base64Images.add("base64Image");
    }
    return base64Images;

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市丙笋,隨后出現(xiàn)的幾起案子谢澈,更是在濱河造成了極大的恐慌,老刑警劉巖御板,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锥忿,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡怠肋,警方通過查閱死者的電腦和手機(jī)敬鬓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來笙各,“玉大人钉答,你說我怎么就攤上這事¤厩溃” “怎么了数尿?”我有些...
    開封第一講書人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)惶楼。 經(jīng)常有香客問我砌创,道長(zhǎng),這世上最難降的妖魔是什么鲫懒? 我笑而不...
    開封第一講書人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任嫩实,我火速辦了婚禮,結(jié)果婚禮上窥岩,老公的妹妹穿的比我還像新娘甲献。我一直安慰自己,他們只是感情好颂翼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開白布晃洒。 她就那樣靜靜地躺著慨灭,像睡著了一般。 火紅的嫁衣襯著肌膚如雪球及。 梳的紋絲不亂的頭發(fā)上氧骤,一...
    開封第一講書人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音吃引,去河邊找鬼筹陵。 笑死,一個(gè)胖子當(dāng)著我的面吹牛镊尺,可吹牛的內(nèi)容都是我干的朦佩。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼庐氮,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼语稠!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起弄砍,我...
    開封第一講書人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤仙畦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后音婶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體议泵,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年桃熄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了先口。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡瞳收,死狀恐怖碉京,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情螟深,我是刑警寧澤谐宙,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站界弧,受9級(jí)特大地震影響凡蜻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜垢箕,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一划栓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧条获,春花似錦忠荞、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽堂油。三九已至,卻和暖如春碧绞,著一層夾襖步出監(jiān)牢的瞬間府框,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工讥邻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留迫靖,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓计维,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親撕予。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鲫惶,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容