简体   繁体   English

如何在Java中打开受密码保护的docx文件?

[英]How to open password protected docx file in java?

I want to open a password protected docx file using Apache POI. 我想使用Apache POI打开受密码保护的docx文件。 Can anyone help me with the complete code please? 有人可以帮我提供完整的代码吗? Am not getting solution with this code 无法获得此代码的解决方案

Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. 线程“主”中的异常org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的数据似乎在Office 2007+ XML中。 You are calling the part of POI that deals with OLE2 Office Documents. 您正在调用POI的OLE2 Office文档。 You need to call a different part of POI to process this data (eg XSSF instead of HSSF) at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:126) at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:113) at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.(NPOIFSFileSystem.java:301) at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:413) at org.apache.poi.hssf.usermodel.HSSFWorkbook.(HSSFWorkbook.java:394) 您需要在org.apache.poi.poifs.storage上的org.apache.poi.poifs.storage.HeaderBlock。(HeaderBlock.java:126)处调用POI的不同部分来处理此数据(例如XSSF而不是HSSF)。 org.apache.poi.poifs.filesystem.NPOIFSFileSystem。(NPOIFSFileSystem.java:301)的.HeaderBlock。(HeaderBlock.java:113)org.apache.poi.hssf.usermodel.HSSFWorkbook。(HSSFWorkbook.java:413)在org.apache.poi.hssf.usermodel.HSSFWorkbook。(HSSFWorkbook.java:394)

  POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("D:/abc.docx"));
    EncryptionInfo info=new EncryptionInfo(fs);
    Decryptor decryptor=Decryptor.getInstance(info);
    if(!decryptor.verifyPassword("user"))
    {
        throw new RuntimeException("document is encrypted");
    }
    InputStream in=decryptor.getDataStream(fs);
    HSSFWorkbook wb=new HSSFWorkbook(in);
    File f=new File("D:/abc5.docx");
    wb.write(f);

The basic code for decryption the XML-based formats of Microsoft Office is shown in XML-based formats - Decryption . 解密Microsoft Office基于XML格式的基本代码以基于XML的格式-Decryption显示

But of course one must know that *.docx , which is a Word file in Office Open XML format, cannot be a HSSFWorkbook , which would be a Excel workbook in binary BIFF file format, but instead must be a XWPFDocument . 但是,当然必须知道*.docx (它是Office Open XML格式的Word文件)不能是HSSFWorkbook ,它可以是二进制BIFF文件格式的Excel工作簿,而必须是XWPFDocument

So: 所以:

import java.io.InputStream;
import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.Decryptor;

import java.security.GeneralSecurityException;

public class ReadEncryptedXWPF {

 static XWPFDocument decryptdocx(POIFSFileSystem filesystem, String password) throws Exception {

  EncryptionInfo info = new EncryptionInfo(filesystem);
  Decryptor d = Decryptor.getInstance(info);

  try {
   if (!d.verifyPassword(password)) {
        throw new RuntimeException("Unable to process: document is encrypted");
   }

   InputStream dataStream = d.getDataStream(filesystem);

   return new XWPFDocument(dataStream);

  } catch (GeneralSecurityException ex) {
    throw new RuntimeException("Unable to process encrypted document", ex);
  }
 }

 public static void main(String[] args) throws Exception {

  POIFSFileSystem filesystem = new POIFSFileSystem(new FileInputStream("abc.docx"));
  XWPFDocument document = decryptdocx(filesystem, "user");

  XWPFWordExtractor extractor = new XWPFWordExtractor(document);
  System.out.println(extractor.getText());
  extractor.close();

 }
}

I have solved this. 我已经解决了。 The code is below 代码如下

    POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("D:/abc.docx"));
    EncryptionInfo info=new EncryptionInfo(fs);
    Decryptor decryptor=Decryptor.getInstance(info);
    XWPFDocument document=null;
    if(decryptor.verifyPassword("password"))
    {
          InputStream dataStream = decryptor.getDataStream(fs); 
          document = new XWPFDocument(dataStream); 
    }else{
        throw new Exception("file is protected with password...please open with right password");
    }
    File f=new File("D:/abc.docx");
    FileOutputStream fos = new FileOutputStream(f);
    document.write(fos);
    document.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM