gpt4 book ai didi

java - URL 连接获取输入流失败

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:14 27 4
gpt4 key购买 nike

我正在尝试查找服务器中目录的大小。所以我已经定义了类来获取服务器的访问权限并找到大小:

public class SoftwareDownload {
private String inputUrl;
private String destinationPath;
private String userName;
private String userPassword;
private URL url = null;
private URLConnection uc = null;
private String userpass;
private String basicAuth;
private boolean isAuthendicated = false;

public SoftwareDownload(String inputUrl, String destinationPath, String userName, String userPassword) {
this.inputUrl = inputUrl;
this.destinationPath = destinationPath;
this.userName = userName;
this.userPassword = userPassword;
userpass = userName + ":" + userPassword;
basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
try {
url = new URL(this.inputUrl);
try {
uc = url.openConnection();
uc.setRequestProperty ("Authorization", basicAuth);
isAuthendicated = true;
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("UC check nul");
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
System.out.println("Connection check nul");
e.printStackTrace();
}
}


public long httpFindSoftwarePackage ( int count,boolean isSizeRequired) throws IOException {
long packageByteCnt = 0;

InputStream inStream = uc.getInputStream();
InputStreamReader streamReader=new InputStreamReader(inStream);
BufferedReader inputStream = new BufferedReader(streamReader);
String line = "";
while(line != null) {
line = inputStream.readLine();
if ( (line != null ) && ( line.startsWith("<a href=")) ) {
Document doc = Jsoup.parse(line);
Element link = doc.select("a").first();
String linkHref = link.attr("href");
String sourceDir = inputUrl+"/"+linkHref;
SoftwareDownload downloadimage = new SoftwareDownload (sourceDir,null,userName,userPassword);
if( linkHref.endsWith("/") == true ) {
packageByteCnt+=downloadimage.httpFindSoftwarePackage(count+1,isSizeRequired);
}
else
{
packageByteCnt+=downloadimage.uc.getContentLengthLong();
}
}
}
return packageByteCnt;
}
}

Class Main:
public static void main(final String[] args) throws IOException {
String httpurl ="http://rb-cmbinex.com/software/rfs/16.2S813/rfs_ni";
String destiDir = "d:/TestDirectory";
String user = "username";
String password = "Password#123";

SoftwareDownload download = new SoftwareDownload(httpurl, null, user, password);

System.out.println("Total Package Size : " + download.httpFindSoftwarePackage(0, true));
}

当我单独运行这个程序时,我可以读取目录的大小。但是,同时当我将这个 SoftwareDownload 调用集成到我的 spring boot java 应用程序时,我得到了这个 IO 异常,

java.io.IOException:服务器返回 HTTP 响应代码:500 用于 URL:http://rb-cmbinex.com/software/rfs/16.2S813/rfs_ni/rootfs/ 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream0(未知来源) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源) 在 com.bosch.boardfarm.model.SoftwareDownload.httpFindSoftwarePackage(SoftwareDownload.java:113) 在 com.bosch.boardfarm.model.SoftwareDownload.httpFindSoftwarePackage(SoftwareDownload.java:143)

然后我发现IO异常来自行(在获取连接的URL的输入流时), InputStream inStream = uc.getInputStream();

有人可以帮助解决这个问题吗。

最佳答案

500 错误意味着:

500 Internal Server Error

A generic error message, given when an unexpected condition was encountered and no more specific message is suitable.

简而言之,您尝试与之通话的服务器出了点问题。您可以通过阅读和打印出错误流的内容来获得更多信息。但是,它可能是空的,或者是一个不提供任何有用信息的“通用”错误页面。


The server is active and running.

你知道这个是因为......? (当我试图与服务器对话时,我只是超时了。)

如果这是您的服务器,请检查日志文件以查看是否有与代码 500 响应对应的日志消息。 (您可能想暂时提高日志记录级别...)

uc.getResponseCode(), uc.getResponseMessage() and uc.getErrorStream() these options are not available in the URLConnection

将连接转换为 HttpURLConnection,然后您可以调用这些方法。

I didn't face this problem when I run this class as a separate program. Suppose there is 10 directory, i am able to traverse first 5 directory , the exception got randomly while traversing the 6 to 10 directory.

这表明了几种可能性:

  • 也许您的请求导致临时过载,并导致内部错误。

  • 也许您的请求导致了暂时的过载,并且服务器正在响应 500 个响应以导致您放慢速度。 (在这种情况下,503 将是更合适的响应。)

  • 也许此响应是一种旨在阻止网络抓取工具的行为!!

  • 也许有一个负载均衡器向多个服务器发送请求,而其中一个(当前)无法正常工作。

关于java - URL 连接获取输入流失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50272423/

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