HttpURLConnection java.io.FileNotFoundException

48

以下代码在连接似乎是Apache服务器时运行良好,但是当我尝试连接到我的.Net服务器时,它会抛出错误。 我猜这是一个头部要求的问题,但无论我尝试什么都无法获得成功的响应。

public String Download(String Url)
{
 String filepath=null;
 try {
  //set the download URL, a url that points to a file on the internet
  //this is the file to be downloaded
  URL url = new URL(Url);
  //create the new connection
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

  //set up some things on the connection
  urlConnection.setRequestMethod("GET");
  urlConnection.setDoOutput(true); 
   //and connect!
  urlConnection.connect();
  //set the path where we want to save the file
  //in this case, going to save it on the root directory of the sd card.
  File SDCardRoot = Environment.getExternalStorageDirectory();
  //create a new file, specifying the path, and the filename
  //which we want to save the file as.

  String filename= "effortback.png";   // you can download to any type of file ex:.jpeg (image) ,.txt(text file),.mp3 (audio file)
  Log.i("Local filename:",""+filename);
  File file = new File(SDCardRoot + "/",filename);

  //=====================================
  if(file.createNewFile())
  {
   file.createNewFile();
  }
  //=====================================

  //this will be used to write the downloaded data into the file we created
  FileOutputStream fileOutput = new FileOutputStream(file);

  //this will be used in reading the data from the internet
  InputStream inputStream = urlConnection.getInputStream();

  //=====================================
  //this is the total size of the file
  int totalSize = urlConnection.getContentLength();
  //variable to store total downloaded bytes
  int downloadedSize = 0;
  //=====================================

  //create a buffer...
  byte[] buffer = new byte[2048];
  int bufferLength = 0; //used to store a temporary size of the buffer

  //now, read through the input buffer and write the contents to the file
  while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
   //add the data in the buffer to the file in the file output stream (the file on the sd card
   fileOutput.write(buffer, 0, bufferLength);
   //add up the size so we know how much is downloaded
   downloadedSize += bufferLength;
   //this is where you would do something to report the prgress, like this maybe
   Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;

  }
  //close the output stream when done
  fileOutput.close();
  if(downloadedSize==totalSize)   filepath=file.getPath();

 //catch some possible errors...
 } catch (MalformedURLException e) {
  e.printStackTrace();
  Log.i("URL-ERROR:",e.toString());
 } catch (IOException e) {
  filepath=null;
  e.printStackTrace();
  Log.i("IO-ERROR:",e.toString());
 }
 Log.i("filepath:"," "+filepath) ;

 return filepath;

}

错误种类包括:

java.io.FileNotFoundException  //I always get this with either one of the below
   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1061)

//or this one below
libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionTmpl.java:186)

看起来无论我尝试什么,它都无法正常工作。如果我尝试从谷歌或其他一些网站下载图像,则可以正常工作,但不是所有网站,并且肯定不是我的(.Net)。我在这里缺失了什么?请帮帮我。


好的,我进行了更深入的挖掘,并发现我从服务器收到了403错误。这是一个安全错误,那么我的问题现在是如何解决它?提前感谢。 - nathan
43
好的,我明白了。如果你遇到相同的问题,非常重要的是不要使用这行代码,还要检查你的URL。首先我的问题是由于URL拼写错误,所以我得到了来自服务器的明显响应。修正了我的URL后,它仍然存在问题。最终,问题出在这一行代码上:urlConnection.setDoOutput(true); 显然,这行JAVA代码强制将HTTP协议从GET更改为POST,而不管是否指定了GET。只是提供给未来有类似问题的人参考。 - nathan
谢谢,这让我忙了一整天。 - Christoffer Klang
谢谢,请回答您自己的问题! - Wotuu
我也卡在这个问题上了。现在它已经正常工作了。谢谢。 - noNameYet
4
@nathan,你应该将这个作为你自己问题的答案发布并接受它。根据SO规则,这完全没有问题,而且以前也做过许多次。 - Alexander Malakhov
3个回答

114

如果您的服务器返回HTTPStatus.BAD_REQUEST(400),则HttpUrlConnection(和OkHttpClient)可能会引发FileNotFoundException。 首先检查状态代码以确定需要读取什么流。

int status = connection.getResponseCode();

if(status >= HttpStatus.SC_BAD_REQUEST)
    in = connection.getErrorStream();
else
    in = connection.getInputStream();

HttpStatus已被弃用,最新语法似乎是:

InputStream inputStream;
int status = urlConnection.getResponseCode();

if (status != HttpURLConnection.HTTP_OK)  {
    inputStream = urlConnection.getErrorStream();
}
else  {
    inputStream = urlConnection.getInputStream();
}

4
太棒了!真的很有帮助。 - Saad Bilal
2
HttpStatus现已被弃用。需要更新此答案。 - The_Martian
1
使用 HttpURLConnection.HTTP_BAD_REQUEST,HTTPStatus.BAD_REQUEST 从 API 级别 22 开始已过时。 - Nikita G.
2
如何打印作为参数传递的内容? - Sahana Prabhakar
代码位于[1608-1624]行sun.net.www.protocol.http: HttpURLConnection.java中。 - Mark Simon

49

我遇到了同样的问题,按照你说的方法解决了:

urlConnection.setDoOutput(false);

请注意,默认情况下它为真,因此您必须将其设置为 false
请注意,您必须将其设置为 false ,因为Volley HurlStack将其设置为true。
注意保重 ;)

编辑: 我刚刚检查了代码,@Abraham Philip说默认情况下它是false。 但是,我必须将其设置为false,因为如果我调用getDoOutput(),它会返回true。 我发现Volley HurlStack将其设置为true。 所以,我的结论是,将setDoOutput设置为false,就可以工作了。

package java.net;
public abstract class URLConnection {
...

    /**
     * Specifies whether this {@code URLConnection} allows sending data.
     */
    protected boolean doOutput;

...
    public boolean getDoOutput() {
        return doOutput;
    }

    public void setDoOutput(boolean newValue) {
        checkNotConnected();
        this.doOutput = newValue;
    }

1
您的陈述似乎是不正确的。setDoOutput默认为false。参考(Java文档):http://download.java.net/jdk7/archive/b123/docs/api/java/net/URLConnection.html#setDoOutput(boolean) - Abraham Philip
是的,你说得对,缺省值默认是false。看看我的更新 ;) - GuilhE
1
并且+1,因为您将OP刚刚作为评论提到的内容转化成了答案。 - Abraham Philip

2
我遇到了一个问题,执行HEAD请求时抛出了FileNotFoundException异常。
原因是,对HEAD的响应没有正文,因此调用getInputStream会抛出FileNotFoundException异常。

因此,如果您正在执行HEAD请求,则不应尝试获取InputStream


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接