gpt4 book ai didi

java - 无法读取文档 URL : Unable to read entire header; 6 bytes read; expected 32 bytes

转载 作者:行者123 更新时间:2023-12-02 09:54:36 25 4
gpt4 key购买 nike

我正在尝试使用 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: 无法读取整个 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://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 方法) 在 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( native 方法) 在 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)

有什么想法吗?

最佳答案

对您的 URL 的初始 HTTP 请求会导致重定向 301 永久移动。因此我们需要处理这个问题并读取新位置。

完整示例:

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

}
}

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

关于java - 无法读取文档 URL : Unable to read entire header; 6 bytes read; expected 32 bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56090668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com