gpt4 book ai didi

Java : class org. apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream 无法转换为类 java.util.zip.ZipFile$ZipFileInputStream

转载 作者:行者123 更新时间:2023-12-05 08:05:44 73 4
gpt4 key购买 nike

我想在我的 excel 文件的特定单元格中写入一些数据,但我总是遇到同样的错误。我使用 Apache POI 写入和读入模板文件:

Exception in thread "Thread-4" org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : class org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream cannot be cast to class java.util.zip.ZipFile$ZipFileInputStream (org.apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream is in unnamed module of loader 'app'; java.util.zip.ZipFile$ZipFileInputStream is in module java.base of loader 'bootstrap') 

主要内容:

private final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
private final File pathTemplate = newFile(Objects.requireNonNull(classLoader.getResource("excel/template.xlsx")).toURI());


public void updateRapport(int indexSheet, int rowwnum, int cellnum, String value, File file) throws IOException, InvalidFormatException {

Workbook workbook = WorkbookFactory.create(new File(file.getPath()));
// Get Sheet
Sheet sheet = workbook.getSheetAt(indexSheet);

System.out.println(sheet.getSheetName());

// Get Row
Row row = sheet.getRow(rowwnum);

// Get the Cell
Cell cell = row.getCell(cellnum);

// Update the cell
cell.setCellType(CellType.STRING);
cell.setCellValue(value);

// Write the output to the file
try(FileOutputStream fileOut = new FileOutputStream(file.getName()))
{
workbook.write(fileOut);
}

// Closing the workbook
workbook.close();
}


public static void main(String[] args) {
updateRapport(0,1,2,"ok",pathTemplate);
}

最佳答案

这是因为当你的资源在 jar 文件中时,你不能把它当作文件。
如果需要文件,将资源内容写到临时文件中使用即可。
像这样:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.junit.Test;

public class FirstTest {
@Test
public void resourceTest() throws IOException {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final InputStream resource = classLoader.getResourceAsStream("resource");
final File file = new File("d:/temp", "fileName");
Files.copy(resource, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}

关于Java : class org. apache.poi.openxml4j.util.ZipSecureFile$ThresholdInputStream 无法转换为类 java.util.zip.ZipFile$ZipFileInputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63853207/

73 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com