gpt4 book ai didi

java - 当我使用 "FileUtils.copyURLToFile(urlConnection.getURL(), exportFile);"时出现IO异常

转载 作者:行者123 更新时间:2023-12-02 00:19:23 25 4
gpt4 key购买 nike

摘要:我正在尝试使用 FileUtils.copyURLToFile(urlConnection.getURL(), exportFile); 保存将从 API 生成的 pdf 文件。但得到401 error .

错误:

java.io.IOException: Server returned HTTP response code: 401 for URL: http://8901-7M0W2X2.KMX.LOCAL/api/comparison/cekdotljl8r98d827gw1y7xvh/result/pdf
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at org.apache.commons.io.FileUtils.copyURLToFile(FileUtils.java:1460)
at sampleAPI.Main.main(Main.java:63)

我已经验证了连接,但遇到了相同的错误。

书面代码:

try {
String name = "****";
String password = "****";
File exportFile = new File(
"C:/New folder/result.pdf");
String webPage = "http://8901-7M0W2X2.KMX.LOCAL:80/api/comparison/compare?file1=file:X/Letter_0.pdf&file1=file:X/Letter_2.pdf&result=pdf&visible=true&profile=MyStyle";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);

FileUtils.copyURLToFile(urlConnection.getURL(), exportFile);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

请提供解决该问题的建议。谢谢!

最佳答案

我可以通过使用InputStream和FileOutPutStream来解决这个问题

response = urlConnection.getInputStream();
fileOut = new FileOutputStream(outputFile);

while ((i = response.read()) != -1) {
fileOut.write(i);
}
fileOut.close();

谢谢!

关于java - 当我使用 "FileUtils.copyURLToFile(urlConnection.getURL(), exportFile);"时出现IO异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58079571/

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