2013-05-31 69 views
0

我試圖保存從a中提取的圖像。 doc文件放在臨時文件夾中。 我能夠創建它的文件夾,也提取了圖像,但我不知道如何將它們保存在文件夾中。 你能幫忙嗎?使用Java將圖片保存在特定的文件夾中

這是迄今已完成:

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

回答

1

使用writeImageContent方法圖片對象。文檔here

writeImageContent

公共無效writeImageContent(java.io.OutputStream中出) java.io.IOException異常

寫入圖片的內容的字節到指定的OutputStream。當需要 將圖片字節直接寫入流時,在內存中省略其表示 作爲不同的字節數組時很有用。參數:out - 寫入 的流拋出:java.io.IOException - 如果發生某種異常,而 寫入指定的輸出

相關問題