【发布时间】:2018-05-10 11:56:56
【问题描述】:
我正在尝试查找服务器中目录的大小。所以我定义了类来获取服务器的访问权并找到大小:
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 for 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();
谁能帮忙解决这个问题。
【问题讨论】:
-
服务器真的在运行吗? Firefox 也无法连接到您的 URL。尝试使用
uc.getResponseCode()、uc.getResponseMessage()和uc.getErrorStream()的组合来找出服务器端实际出了什么问题。很有可能服务器不允许 http 连接或不允许列出目录内容。 -
服务器处于活动状态并正在运行。 uc.getResponseCode()、uc.getResponseMessage() 和 uc.getErrorStream() 这些选项在 URLConnection 中不可用。当我将这个类作为一个单独的程序运行时,我没有遇到这个问题。假设有 10 个目录,我可以遍历第 5 个目录,在遍历 6 到 10 个目录时随机出现异常。
标签: java http spring-boot url