【问题标题】:Save pictures in a specific folder with Java使用 Java 将图片保存在特定文件夹中
【发布时间】:2013-05-27 08:06:58
【问题描述】:

我正在尝试保存从 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);
}

【问题讨论】:

    标签: java save directory image fileutils


    【解决方案1】:

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

    写入图像内容

    public void writeImageContent(java.io.OutputStream out) 抛出 java.io.IOException

    将图片的内容字节写入指定的输出流。有需要的时候有用 将图片字节直接写入流,省略其表示 在内存中作为不同的字节数组。参数: out - 要写入的流 抛出:java.io.IOException - 如果发生了一些异常同时 写到指定的地方

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 2013-08-03
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多