无法读取文档 URL:无法读取整个 header;读取 6 个字节;预计 32 字节

doc URL cannot be read: Unable to read entire header; 6 bytes read; expected 32 bytes

我正在尝试使用 POI 3.6 版从 Web URL 读取 Word 文档。 Non-working代码:

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:无法读取整个 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。出现的消息是: 资源解释为文档但使用 MIME 类型传输 application/msword:“https://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc”。

我还是卡住了...

更新:正如 https://whosebug.com/users/3915431/axel-richter 指出的那样,有一个 301 重定向到 https://prevention.cancer.gov/sites/default/files/uploads/clinical_trial/Master-DMP-Template.doc 。但是,现在我 运行 遇到了与 Word 无关的奇怪问题。 Followig 代码失败:

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(本机方法) 在 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)

有什么想法吗?

这段代码 new URL(urlString).openStream() return InputStream look here 而不是像这样的 FileInputStream:

InputStream inputStream = new FileInputStream("C:\Users\me\Downloads\Master...")

也许这个差异有问题?

对您 URL 的初始 HTTP 请求导致重定向 301 Moved Permanently。所以我们需要处理这个并读取新位置。

完整示例:

import java.io.InputStream;
import java.net.URL;
import java.net.HttpURLConnection;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

public class OpenHWPFFromURL {

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

  String stringURL = "http://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(responseCode); //301 Moved Permanently

  if (responseCode != HttpURLConnection.HTTP_OK) {
   if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP
       || responseCode == HttpURLConnection.HTTP_MOVED_PERM
       || responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
    url = new URL(con.getHeaderField("Location")); //get new location
    con = (HttpURLConnection)url.openConnection();
   }   
  }

  InputStream in = con.getInputStream();
  HWPFDocument doc = new HWPFDocument(in);
  WordExtractor extractor = new WordExtractor(doc);
  String text = extractor.getText();

  System.out.println(text);

 }
}

注意:如果重定向也更改了协议(从 HTTPHTTPS 例如)。这里也正是这种情况。所以我们需要手动获取新位置,如我的代码所示。