【问题标题】:URL connection get input stream failsURL 连接获取输入流失败
【发布时间】: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


【解决方案1】:

500 错误意味着:

500 内部服务器错误

一般错误消息,当遇到意外情况且没有更具体的消息适合时给出。

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


服务器处于活动状态并正在运行。

你知道这是因为.....? (当我尝试与服务器交谈时,我只是超时了。)

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

uc.getResponseCode()uc.getResponseMessage()uc.getErrorStream() 这些选项在 URLConnection 中不可用

将连接投射到HttpURLConnection,然后您就可以调用这些方法了。

当我将这个类作为一个单独的程序运行时,我没有遇到这个问题。假设有 10 个目录,我可以遍历前 5 个目录,在遍历 6 到 10 目录时随机出现异常。

这表明了几种可能性:

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

  • 也许您的请求导致临时过载,服务器响应 500 个响应导致您变慢。 (在这种情况下,503 将是更合适的响应。)

  • 也许这种反应是一种旨在阻止网络爬虫的行为!

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

【讨论】:

  • 我得到的响应码和消息分别是500和“Internal Server Error”。
  • 如果服务器真的超载,当我将此代码作为单独的程序运行时,我没有收到此错误。但是,当我在我的 Spring Boot 应用程序中使用此代码时(这是 ajax .post 方法函数)引发异常。
  • 您之前说过您从命令行运行程序一次,在 Spring 中运行多次。 “很多时候”表明 >> 你
  • 好的。所以你认为这是SpringBoot的错。因此,捕获两种不同情况下通过网络发送的请求,并找出它们之间的区别。
  • 我认为你有足够的工作要做。您只需要仔细并有逻辑地检查所有可用的证据......并避免得出没有证据支持的结论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多