Java工具类记录

HTML转word

相关依赖

<dependency>
     <groupId>org.apache.poigroupId>
     <artifactId>poiartifactId>
     <version>4.1.2version>
 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文档
 引入如下依赖
 
     org.apache.poi
     poi
     4.1.2
 
 */
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.jsoupgroupId>
    <artifactId>jsoupartifactId>
    <version>1.14.2version>
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 "\n" +
                "\n" +
                " \n" +
                " \n" +
                "菜鸟教程(runoob.com) \n" +
                "\n" +
                "\n" +
                "

Norwegian Mountain Trip

\n"
+ "\"Pulpit\n" + "\n" + ""; } }

压缩文件

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);
                }
            }
        }
    }
}

你可能感兴趣的:(java)