【问题标题】:Java, FileNotfound Exception, While reading conn.getInputStream()Java,FileNotfound 异常,读取 conn.getInputStream() 时
【发布时间】:2013-01-29 12:04:11
【问题描述】:

请告诉我一些,如何解决这个问题, 有时我收到 Filenotfound 异常,有时这段代码运行良好。

下面是我的代码,

public String sendSMS(String data, String url1) {
            URL url;

            String status = "Somthing wrong ";
            try {
                url = new URL(url1);
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
                conn.setRequestProperty("Accept","*/*");
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(data);
                wr.flush();

                // Get the response
                try {
                    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String s;
                    while ((s = rd.readLine()) != null) {
                        status = s;
                    }
                    rd.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                wr.close();

            } catch (MalformedURLException e) {
                status = "MalformedURLException Exception in sendSMS";
                e.printStackTrace();
            } catch (IOException e) {
                status = "IO Exception in sendSMS";
                e.printStackTrace();
            }

            return status;
        }

【问题讨论】:

  • 你能显示堆栈跟踪和你得到的错误信息吗?
  • url1的值是多少?
  • 您是否在异常消息中看到“打开的文件过多”?
  • 尝试编写 wr.close();紧接在 wr.flush() 之后

标签: java http-post


【解决方案1】:

像这样重写,让我知道你是怎么做的……(注意关闭读写流,如果抛出异常,还要清理流)。

public String sendSMS(String data, String url1) {
    URL url;
    OutputStreamWriter wr = null;
    BufferedReader rd = null;

    String status = "Somthing wrong ";

    try {

        url = new URL(url1);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
        conn.setRequestProperty("Accept","*/*");

        wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();
        wr.close();

        rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String s;
        while ((s = rd.readLine()) != null) {
            status = s;
        }
        rd.close();                

    } catch (Exception e) {
        if (wr != null) try { wr.close(); } catch (Exception x) {/*cleanup*/}
        if (rd != null) try { rd.close(); } catch (Exception x) {/*cleanup*/}
        e.printStackTrace();
    }    

    return status;
}

【讨论】:

    【解决方案2】:

    这个问题似乎是已知的,但由于不同的原因,所以不清楚为什么会发生这种情况。

    有些线程会建议关闭 OutputStreamWriter,因为刷新它是不够的,因此我会尝试在刷新后直接关闭它,因为您没有在刷新和关闭之间的代码中使用它。

    其他线程表明使用不同的连接(如 HttpURLConnection)可以避免发生此问题(看看here

    Another article 建议使用 URLEncoder 类的静态方法 encode。此方法接受一个字符串并将其编码为可以放入 URL 的字符串。

    一些类似的问题:

    URL is accessable with browser but still FileNotFoundException with URLConnection

    URLConnection FileNotFoundException for non-standard HTTP port sources

    URLConnection throwing FileNotFoundException

    祝你好运。

    【讨论】:

      【解决方案3】:

      当服务器对 HTTP 请求的响应是代码 404 时返回 FileNotFoundException。

      检查您的网址。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多