Path.of上面的Path.of报错了Cannot resolve method ‘of‘ in ‘Path‘更换java8也是会报错Cannot resolve method ‘of‘ in ‘P

Path.of上面的Path.of报错了Cannot resolve method 'of' in 'Path'更换java8也是会报错Cannot resolve method 'of' in 'Path'请换一种方式实现java读取读取url如http://18.0.0.9:8080/test/wf.tar,这个url是一个下载连接会下载一个不带文件类型的压缩包里面有两个文件,java怎么样实现读取这个url把重命名的压缩包的里面的两个文件的每个文件MD5值返回 不用Path.of

如果你使用的是 Java 8,Path.of 应该是可以用的。但如果仍然出现问题,可以尝试使用 Paths.get 来代替。下面是一个简单的例子,演示如何从 URL 中下载压缩包,解压并计算每个文件的 MD5 值:

import java.io.InputStream;
import java.net.URL;
import java.nio.file.*;
import java.security.MessageDigest;

public class Main {
    public static void main(String[] args) {
        try {
            // 定义 URL 和本地文件名
            String url = "http://18.0.0.9:8080/test/wf.tar";
            String localFileName = "downloadedFile.tar";

            // 下载文件
            downloadFile(url, localFileName);

            // 解压文件
            String extractedDir = "extractedFiles";
            extractTarGz(localFileName, extractedDir);

            // 计算 MD5
            calculateMD5(extractedDir);

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

    private static void downloadFile(String url, String localFileName) throws Exception {
        try (InputStream in = new URL(url).openStream()) {
            Files.copy(in, Paths.get(localFileName), StandardCopyOption.REPLACE_EXISTING);
        }
    }

    private static void extractTarGz(String archivePath, String destDir) throws Exception {
        try (InputStream in = Files.newInputStream(Paths.get(archivePath));
             GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(in);
             TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {

            ArchiveEntry entry;
            while ((entry = tarIn.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    continue;
                }

                Path entryPath = Paths.get(destDir, entry.getName());
                Files.copy(tarIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }

    private static void calculateMD5(String directory) throws Exception {
        Files.walk(Paths.get(directory))
                .filter(Files::isRegularFile)
                .forEach(file -> {
                    try {
                        byte[] fileBytes = Files.readAllBytes(file);
                        String md5 = calculateMD5(fileBytes);
                        System.out.println("File: " + file.getFileName() + ", MD5: " + md5);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
    }

    private static String calculateMD5(byte[] data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(data);
        byte[] mdBytes = md.digest();

        // convert the byte to hex format
        StringBuilder sb = new StringBuilder();
        for (byte mdByte : mdBytes) {
            sb.append(Integer.toString((mdByte & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }
}

上面的代码中使用了 Paths.get 来代替 Path.of,同时引入了 GzipCompressorInputStreamTarArchiveInputStream 类来处理 tar.gz 压缩包。确保你的项目中已经包含了这些依赖。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我之前给出的代码可能有误。PDFRenderer 类确实没有 setDocument 方法。我给你提供的示例代码中的错误是由于错误地使用了 PDFRenderer 类。 在 Apache PDFBox 中,你可以使用 org.apache.pdfbox.pdmodel.PDPageContentStream 类的 drawXObject 方法来将 HTML 文件转换为 PDF。下面是更新后的示例代码: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.PDRectangle; public class HTMLtoPDFConverter { public static void main(String[] args) { String htmlFilePath = "path/to/input.html"; String pdfFilePath = "path/to/output.pdf"; // Load HTML file File htmlFile = new File(htmlFilePath); try { // Create a new PDF document PDDocument document = new PDDocument(); PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); // Create a PDPageContentStream object PDPageContentStream contentStream = new PDPageContentStream(document, page); // Load the HTML file content String htmlContent = org.apache.commons.io.FileUtils.readFileToString(htmlFile, StandardCharsets.UTF_8); // Create a temporary file to hold the HTML content File tempHtmlFile = File.createTempFile("temp", ".html"); org.apache.commons.io.FileUtils.writeStringToFile(tempHtmlFile, htmlContent, StandardCharsets.UTF_8); // Convert the HTML file to image PDImageXObject image = org.apache.pdfbox.rendering.HTMLRendererUtil.convertToImage(document, tempHtmlFile); // Draw the image on the PDF contentStream.drawImage(image, 0, 0, PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight()); // Close the content stream and the document contentStream.close(); document.save(new FileOutputStream(pdfFilePath)); document.close(); // Delete the temporary HTML file tempHtmlFile.delete(); System.out.println("HTML converted to PDF successfully."); } catch (IOException e) { e.printStackTrace(); } } } ``` 请确保将 `path/to/input.html` 替换为要转换的 HTML 文件的路径,将 `path/to/output.pdf` 替换为保存生成的 PDF 文件的路径。运行此代码将生成一个包含 HTML 内容的 PDF 文件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值