Java工具类记录

HTML转word

相关依赖

<dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi</artifactId>
     <version>4.1.2</version>
 </dependency>
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 *
 html转成word文档
 引入如下依赖
 <dependency>
     <groupId>org.apache.poi</groupId>
     <artifactId>poi</artifactId>
     <version>4.1.2</version>
 </dependency>
 */
public class HtmlToWordUtils {

    public static void main(String[] args) throws Exception {
        String htmlFile = "D:\\2023\\code\\javaCode\\src\\main\\resources\\test.html";
        String wordFile = "D:\\2023\\code\\javaCode\\src\\main\\java\\test\\htmlToWord\\abc.doc";
        htmlToDocToLocal(readFileContent(htmlFile), wordFile);
    }

    /**
     * @param richText 富文本内容
     * @param localFilePath 输出文件地址
     * @throws Exception
     */
    public static void htmlToDocToLocal(String richText, String localFilePath) throws Exception {
        // 设置编码
        byte[] bytes = richText.getBytes("UTF-8");
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);){
            POIFSFileSystem poifs = new POIFSFileSystem();
            // ##############下面这两个不能删掉
            DirectoryEntry directory = poifs.getRoot();
            DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
            // ################这两个不能删掉
            // 输出到本地文件
            try (OutputStream ostream = new FileOutputStream(localFilePath);){
                poifs.writeFilesystem(ostream);
            }
        }

    }

    // 读取文件内容
    public static String readFileContent(String filePath) throws Exception {
        StringBuilder sb = new StringBuilder();
        try (FileInputStream fis = new FileInputStream(filePath)) {
            byte[] buffer = new byte[1024];
            int readBytes;
            while ((readBytes = fis.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, readBytes, StandardCharsets.UTF_8));
            }
        }
        return sb.toString();
    }
}

解析HTML文件

相关依赖

<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.14.2</version>
</dependency>
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class ParseHtml {
    public static void main(String[] args) {
        String html = getHtml();
        Document document = Jsoup.parse(html);
        // 获取所有的img标签
        Elements imgs = document.getElementsByTag("img");
        for (Element img : imgs) {
            // 拿到src属性值
            String src = img.attr("src");
            System.out.println(src);
            // 修改src属性值
            img.attr("src", "xxxxxx");
        }
        System.out.println(document.html());
    }

    private static String getHtml() {
        return "<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head> \n" +
                "<meta charset=\"utf-8\"> \n" +
                "<title>菜鸟教程(runoob.com)</title> \n" +
                "</head>\n" +
                "<body>\n" +
                "<h2>Norwegian Mountain Trip</h2>\n" +
                "<img border=\"0\" src=\"/images/pulpit.jpg\" alt=\"Pulpit rock\" width=\"304\" height=\"228\">\n" +
                "</body>\n" +
                "</html>";
    }
}

压缩文件

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFileUtils {
    public static void main(String[] args) throws Exception {
        // 压缩后的文件
        String zipFile = "D:\\lzc\\example.zip";
        // 需要压缩的文件夹
        String folderToCompress = "D:\\lzc\\ab";
        compress(zipFile, folderToCompress, true);
    }

    /**
     * @param zipFile 压缩包名称
     * @param folderName 压缩文件夹
     * @param recursive 是否需要递归压缩
     */
    public static void compress(String zipFile, String folderName, boolean recursive) throws IOException {
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFile))) {
            if (recursive) {
                // 需要递归压缩文件夹下所有的文件+目录
                compressFolder(folderName, folderName, zipOutputStream);
            } else {
                // 只压缩当前文件夹的文件
                compressCurrentFolder(folderName, zipOutputStream);
            }
        }
    }


    /**
     * 只压缩当前文件夹下的文件
     * @param folderName
     * @param zipOutputStream
     * @throws IOException
     */
    public static void compressCurrentFolder(String folderName, ZipOutputStream zipOutputStream) throws IOException {
        File folder = new File(folderName);
        File[] files = folder.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                continue;
            }
            addToZipFile(file.getName(), file.getAbsolutePath(), zipOutputStream);
        }
    }

    /**
     * 递归遍历所有的文件并打包
     * @param sourceFolder 要压缩的文件夹
     * @param folderName 需要递归的文件夹
     * @param zipOutputStream 压缩文件
     * @throws IOException
     */
    public static void compressFolder(String sourceFolder, String folderName, ZipOutputStream zipOutputStream) throws IOException {
        File folder = new File(folderName);
        File[] files = folder.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            if (file.isDirectory()) {
                // 压缩子文件夹
                compressFolder(sourceFolder, file.getAbsolutePath(), zipOutputStream);
            } else {
                // 压缩文件
                String absolutePath = file.getAbsolutePath();
                addToZipFile(absolutePath.replace(sourceFolder,""), absolutePath, zipOutputStream);
            }
        }
    }

    /**
     * 将文件添加到压缩包中
     * @param fileName
     * @param fileAbsolutePath
     * @param zipOutputStream
     * @throws IOException
     */
    public static void addToZipFile(String fileName, String fileAbsolutePath, ZipOutputStream zipOutputStream) throws IOException {
        // 创建ZipEntry对象并设置文件名
        ZipEntry entry = new ZipEntry(fileName);
        zipOutputStream.putNextEntry(entry);
        // 读取文件内容并写入Zip文件
        try (FileInputStream fileInputStream = new FileInputStream(fileAbsolutePath)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                zipOutputStream.write(buffer, 0, bytesRead);
            }
        }
        // 完成当前文件的压缩
        zipOutputStream.closeEntry();
    }
}

图片工具类

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;

public class ImageUtils {
    /**
     * 将本地文件转换成HTML中img标签能识别的src
     * @param filePath 图片地址
     * @return
     * @throws IOException
     */
    public static String localImageToHtmlImgBase64(String filePath) throws IOException {
        File file = new File(filePath);
        String fileName = file.getName();
        String imageType = fileName.substring(fileName.lastIndexOf(".") + 1);
        return "data:image/" + imageType + ";base64," + localFileToBase64(filePath);
    }

    /**
     * 将本地文件转换成base64
     * @param filePath 文件地址
     * @return
     */
    public static String localFileToBase64(String filePath) throws IOException {
        try (InputStream inputStream = new FileInputStream(filePath)){
            int available = inputStream.available();
            byte[] bytes = new byte[available];
            inputStream.read(bytes);
            return Base64.getEncoder().encodeToString(bytes);
        }
    }

    /**
     * 将图片下载到本地
     * @param imageUrl
     * @param localFilePath
     * @throws IOException
     */
    public static void imageUrlToLocalFile(String imageUrl, String localFilePath) throws IOException {
        // 打开连接
        URL url = new URL(imageUrl);
        URLConnection connection = url.openConnection();
        // 设置请求超时为5秒
        connection.setConnectTimeout(5 * 1000);
        // 读取数据流并保存到本地
        try (InputStream input = connection.getInputStream()){
            byte[] data = new byte[1024];
            int len;
            try (FileOutputStream output = new FileOutputStream(localFilePath)){
                while ((len = input.read(data)) != -1) {
                    output.write(data, 0, len);
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值