【发布时间】:2019-05-11 13:12:15
【问题描述】:
我正在尝试使用 POI 3.6 版从 Web URL 读取 Word 文档。非工作代码:
String url = "http://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc";
InputStream inputStream = new URL(urlString).openStream();
HWPFDocument doc = new HWPFDocument(inputStream);
WordExtractor extractor = new WordExtractor(doc);
String text = extractor.getText();
以上代码导致 java.io.IOException: Unable to read entire header;读取 6 个字节;预计 32 字节
尝试2:有趣的是下载文件(只是将URL粘贴到浏览器地址栏中),然后执行类似的代码以在本地读取文档:
InputStream inputStream = new FileInputStream("C:\\Users\\me\\Downloads\\Master-DMP-Template (2).doc");
HWPFDocument doc = new HWPFDocument(inputStream);
WordExtractor extractor = new WordExtractor(doc);
System.out.println(extractor.getText());
尝试 3:现在是最奇怪的部分。我认为需要先下载文件。所以我先用Java下载了它,然后执行前面的代码在本地读取文档。像第一个案例一样失败!
final String url = "http://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc";
String localPath = FileUtils.downloadFile("C:\\Users\\me\\Downloads", url);
InputStream inputStream = new FileInputStream(localPath);
HWPFDocument doc = new HWPFDocument(inputStream);
WordExtractor extractor = new WordExtractor(doc);
System.out.println(extractor.getText());
public static String downloadFile(String targetDir, String sourceUrl) throws IOException {
sourceUrl = StringUtils.removeEnd(sourceUrl, "/");
String fileName = sourceUrl.substring(sourceUrl.lastIndexOf("/") + 1);
String targetPath = targetDir + FileUtils.SEPARATOR + fileName;
InputStream in = new URL(sourceUrl).openStream();
Files.copy(in, Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING);
System.out.println("Downloaded " + sourceUrl + " to " + targetPath);
return targetPath;
}
知道这里发生了什么吗?
更新:我创建了一个单独的项目来尝试使用 POI 4.1.0。相同的代码(第一次尝试)导致 org.apache.poi.EmptyFileException:提供的文件是空的(零字节长)
我尝试在按 F12 并观察“网络”选项卡后将 URL 粘贴到浏览器中。那里出现的消息是: 资源解释为 Document,但使用 MIME 类型 application/msword 传输:“https://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc”。
我还是卡住了……
更新:正如 https://stackoverflow.com/users/3915431/axel-richter 所指出的,有一个 301 重定向到 https://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc 。但是,现在我遇到了与 Word 无关的奇怪问题。以下代码失败:
public static void main(String[] args) {
try {
if (args.length > 0 && args[0].equals("disableCertValidation")) {
SSLUtil.disableCertificateValidation(); // redirect is https
}
final String stringURL = "https://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc";
URL url = new URL(stringURL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int responseCode = con.getResponseCode();
System.out.println("Response code: " + responseCode); //301 Moved Permanently
InputStream in = con.getInputStream();
HWPFDocument doc = new HWPFDocument(in);
WordExtractor extractor = new WordExtractor(doc);
String text = extractor.getText();
System.out.println(text);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
在没有参数的情况下运行 main 时,行
int responseCode = con.getResponseCode();
失败并出现以下异常: javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
使用 disableCertificateValidation 参数运行代码时,响应代码为 404,我收到以下异常:
java.io.FileNotFoundException: https://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:422) 在 sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1890) 在 sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1885) 在 java.security.AccessController.doPrivileged(本机方法) 在 sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1884) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1457) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 在 com.keywords.control.util.TestHTMLParser.main(TestHTMLParser.java:472) 引起:java.io.FileNotFoundException:https://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1836) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) 在 java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338) 在 com.keywords.control.util.TestHTMLParser.main(TestHTMLParser.java:470)
有什么想法吗?
【问题讨论】:
标签: java apache-poi doc