gpt4 book ai didi

java - 用Java将图片保存到特定文件夹中

转载 作者:行者123 更新时间:2023-12-01 14:31:05 25 4
gpt4 key购买 nike

我正在尝试保存从 a 中提取的图像。临时文件夹中的 doc 文件。我能够创建它的文件夹,并且图像也被提取,但我不知道如何将它们保存在文件夹中。你能帮忙吗?

这是迄今为止所做的事情:

private static void documentImagesCapture(HWPFDocument doc) {
PicturesTable picturesTable = doc.getPicturesTable();
List<Picture> allPictures = picturesTable.getAllPictures();
System.out.println("Number of pictures in the document: "+allPictures.size());

if (allPictures.size()>0){

final Path folderPath = Paths.get("src/test/resources/test-documents/");
final Path tmpDir;

try {
// Create tmp folder
tmpDir = Files.createTempDirectory(folderPath, null);
System.out.println("This is a temporary folder: " + tmpDir);

// Extract a Picture file
for (Picture pic : allPictures){
System.out.println("this is the picture that I want to save : " + pic);
//Save pic in tmpDir???????
}

} catch (IOException e) {
System.err.println(">>>>>>>>> ERROR >>>>>>>>> WordDocSplitter.documentImagesCapture()");
e.printStackTrace();
}
}
}

谢谢

<小时/>

谢谢大卫!!!!我是这样解决的:

for (Picture pic : allPictures){
File newFilePic = new File(pic.suggestFullFileName());
pic.writeImageContent(new DataOutputStream(new FileOutputStream(newFilePic)));
FileUtils.copyFileToDirectory(newFilePic, new File(tmpDir.toString()));
FileUtils.forceDelete(newFilePic);
}

最佳答案

使用 Picture 对象的 writeImageContent 方法。文档 here .

writeImageContent

public void writeImageContent(java.io.OutputStream out) throws java.io.IOException

Writes Picture's content bytes to specified OutputStream. Is useful when there is need to write picture bytes directly to stream, omitting its representation in memory as distinct byte array. Parameters: out - a stream to write to Throws: java.io.IOException - if some exception is occured while writing to specified out

关于java - 用Java将图片保存到特定文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16858355/

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