- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在抓狂,因为我无法让 sample 发挥作用 - 希望有人可以提供帮助..我想检测 docx 和 doc 文件是否受密码保护/加密。我在一些地方看到过这个帖子,但我无法让它工作 - 它不会抛出异常。有人可以看到我做错了什么吗?注意我只需要检测密码..我不想打开文档。
String fileLocation = "C:/myfile.docx";
File file = new File(fileLocation);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
POIFSFileSystem pfis = new POIFSFileSystem(fis);
try{
EncryptionInfo info = new EncryptionInfo(pfis);
EncryptionMode mode = info.getEncryptionMode();
Decryptor d = Decryptor.getInstance(info);
//Try and open it
if(!d.verifyPassword(Decryptor.DEFAULT_PASSWORD))
{
//file is encrypted
}
}
catch(GeneralSecurityException gse)
{
//file is encrypted
}
catch(EncryptedDocumentException edc)
{
//file is encrypted
}
最佳答案
我没有详细说明如何缩小代码,但我只是采用了工厂类之一 - 例如 SlideShowFactory - 并针对 H/XWPF 进行了修改。由于 H/XWPF 还没有文档级别的通用接口(interface)(到目前为止),快速而肮脏的方法是返回一个对象。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class EncDetect {
public static void main(String[] args) {
String dir = "/home/kiwiwings/project/poi/poi/test-data";
String[] files = {
"document/bug53475-password-is-solrcell.docx",
"document/password_tika_binaryrc4.doc",
"document/58067.docx",
"document/58804.doc"
};
for (String f : files) {
try {
DocumentFactory.create(new File(dir, f));
System.out.println(f + " not encrypted");
} catch (EncryptedDocumentException e) {
System.out.println(f + " is encrypted");
} catch (Exception e) {
System.out.println(f + " " +e.getMessage());
}
}
}
static class DocumentFactory {
public static Object create(NPOIFSFileSystem fs) throws IOException {
return create(fs, null);
}
public static Object create(final NPOIFSFileSystem fs, String password) throws IOException {
DirectoryNode root = fs.getRoot();
// Encrypted OOXML files go inside OLE2 containers, is this one?
if (root.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) {
InputStream stream = null;
try {
stream = DocumentFactoryHelper.getDecryptedStream(fs, password);
return createXWPFDocument(stream);
} finally {
IOUtils.closeQuietly(stream);
}
}
// If we get here, it isn't an encrypted XWPF file
// So, treat it as a regular HWPF one
boolean passwordSet = false;
if (password != null) {
Biff8EncryptionKey.setCurrentUserPassword(password);
passwordSet = true;
}
try {
return createHWPFDocument(fs);
} finally {
if (passwordSet) {
Biff8EncryptionKey.setCurrentUserPassword(null);
}
}
}
public static Object create(InputStream inp) throws IOException, EncryptedDocumentException {
return create(inp, null);
}
public static Object create(InputStream inp, String password) throws IOException, EncryptedDocumentException {
// If clearly doesn't do mark/reset, wrap up
if (! inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
// Ensure that there is at least some data there
byte[] header8 = IOUtils.peekFirst8Bytes(inp);
// Try to create
if (NPOIFSFileSystem.hasPOIFSHeader(header8)) {
NPOIFSFileSystem fs = new NPOIFSFileSystem(inp);
return create(fs, password);
}
if (DocumentFactoryHelper.hasOOXMLHeader(inp)) {
return createXWPFDocument(inp);
}
throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}
public static Object create(File file) throws IOException, EncryptedDocumentException {
return create(file, null);
}
public static Object create(File file, String password) throws IOException, EncryptedDocumentException {
return create(file, password, false);
}
public static Object create(File file, String password, boolean readOnly) throws IOException, EncryptedDocumentException {
if (!file.exists()) {
throw new FileNotFoundException(file.toString());
}
NPOIFSFileSystem fs = null;
try {
fs = new NPOIFSFileSystem(file, readOnly);
return create(fs, password);
} catch(OfficeXmlFileException e) {
IOUtils.closeQuietly(fs);
return createXWPFDocument(file, readOnly);
} catch(RuntimeException e) {
IOUtils.closeQuietly(fs);
throw e;
}
}
protected static Object createHWPFDocument(NPOIFSFileSystem fs) throws IOException, EncryptedDocumentException {
return new HWPFDocument(fs.getRoot());
}
protected static Object createXWPFDocument(InputStream stream) throws IOException, EncryptedDocumentException {
return new XWPFDocument(stream);
}
protected static Object createXWPFDocument(File file, boolean readOnly) throws IOException, EncryptedDocumentException {
try {
OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE);
return new XWPFDocument(pkg);
} catch (InvalidFormatException e) {
throw new IOException(e);
}
}
}
}
关于Word DocX 上的 Java DescriptionInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45238486/
我有一个来自 C# 库的 Paragraph 对象 DocX并尝试设置 LineSpacing 属性但没有任何效果? internal static Paragraph StandardFormat(
我正在寻找一种方法来提取文档中每个单词的位置 (x, y) 和属性(字体/大小)。 从 python-docx 文档中,我知道: Conceptually, Word documents have t
我有一个带有空表的 .docx 模板,我要在其中添加值: def manipulate_table(): table = doc.tables[0] table.cell(0, 0).text = '
我目前正在开发一个将 docx 文件作为输入并使用它来构建 html 页面的库,由于 docx 的模糊和缺乏文档,我不得不严重依赖示例输出来决定如何处理某些事情。其中之一是超链接。 就我目前所见,do
使用以下代码,我尝试创建一个文档,其中第 2 页和第 3 页为横向,而其他页为纵向。所有尺寸都应为 8.5"x 11"。 using (System.IO.MemoryStream ms = new
我使用了 python-docx 中的示例,在运行代码后,我找不到 docx 文件在哪里,我可以指出要添加的特定路径吗? from docx import Document from docx.sha
我对 python-docx 中“运行级别内容”的概念有些困惑。我明白,如果我想检查一个段落是否以粗体显示,我需要检查 run.bold,但究竟是什么它? 官方定义是:运行是与内联内容最密切相关的对象
我了解到 .docx 文件基本上是二进制文件。但我不知道下面的结构。 .docx 文件的基本结构是什么?比如,标题有多长?实际的文档内容从什么时候开始?最后有签名吗? 基本上,.docx 文件的结构是
我正在尝试将 .adoc 文件转换为 .docx 实际上我正在使用: asciidoctor file.adoc -o file.html pandoc -s -S file.html -o outp
我可能错过了一些东西或犯了一个错误,无论如何,我似乎无法访问 .docx 模板中字典中的数据。文档说它的工作方式类似于 jinja2,但使用 {{ dict['dict_key'] }} 即使在 if
我遇到了有关如何使用 C# 合并 docx 文件的解决方案: Append multiple DOCX files together 在此解决方案中,他遍历文件并将正文“outerxml”复制到新文档
我正在使用 Docx dll 获取段落信息但无法获取段落格式(字体大小-字体名称-字体颜色) 这是我的代码: using (DocX document = DocX.Load("Tes
使用 Markdown 代码块时,DOCX 文档中生成的等宽字体大小太大。 我可以通过指定自定义 template.docx 文件来调整段落的字体大小,但由于某种原因,生成的代码块不使用段落样式,这与
doc=Document() table = doc.add_table(rows = 13, cols = 5) table.style = 'Table Grid' row = table.row
我想打开一个现有的 Word 文档,我已经在其中添加了页码,然后向其中添加了一些文本和标题。 这是我如何尝试实现目标的基本示例 #!/usr/bin/env python from docx impo
我想使用 DocX Library 将图像添加到 C# 中的 Word 文件中.问题是我在网上找不到任何东西。 情况 我知道如何创建文件,我知道如何在文件中写入文本。遗憾的是,图书馆的文档非常小。希望
我已经下载并开始使用 DocX library .我有一个名为 template.docx 的文档正在加载到内存中。我在该文档中有一个表,其中包含 id = 241。我想通过它的 id 获取该表并向其
是否可以使用应用了样式的 python-docx 将 HTML 插入到文档中?我唯一需要做的就是斜体。 例如如何插入"Today is Saturday."星期六实际上是用斜体插入的吗? 谢谢! 最佳
我想在命令行 (Linux) 中执行此操作,这样我就可以自动执行它,而无需在中间设置用户界面。 最佳答案 WordprocessingML 中没有明确的页面布局模型。 但是,如果文件最后是由 Word
我必须以 docx 格式存储一些文档,但无法忍受使用 msword:我想编辑某种纯文本标记,除了基于 XML 的东西(我也不喜欢那样)和从/到那个到/从 docx 转换。 有什么选择吗? 编辑:由于人
我是一名优秀的程序员,十分优秀!