Android HttpURLConnection java.io.FileNotFoundException

Android HttpURLConnection java.io.FileNotFoundException,android,download,httpurlconnection,filenotfoundexception,Android,Download,Httpurlconnection,Filenotfoundexception,如果我连接到似乎是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

如果我连接到似乎是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;

}
urlConnection.setDoOutput(false);
错误范围为:

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)
urlConnection.setDoOutput(false);

看来无论我怎么努力,我都无法使它发挥作用。同样,如果我尝试从谷歌或其他一些网站下载一张图片,但不是全部,但绝对不是我的(.Net)。我错过了什么?请帮助。

遇到了相同的问题,如您所说解决了:

urlConnection.setDoOutput(false);
请注意,您必须将其设置为false,因为默认情况下它为true。
请注意,您必须将其设置为false,因为
Volley
HurlStack
正在将其设置为true。
注意;)

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

urlConnection.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;
    }
如果您的服务器返回=
(400),您可以从
HttpUrlConnection
(和
OkHttpClient
)获取
FileNotFoundException
)。您应该首先检查状态代码,以检查需要读取的流。

urlConnection.setDoOutput(false);

urlConnection.setDoOutput(false);
HttpStatus已弃用。最新语法似乎是:
我遇到了一个问题,执行
HEAD
-请求时抛出了
FileNotFoundException

原因是,对
头的响应没有主体,因此调用
getInputStream
会抛出
FileNotFoundException

urlConnection.setDoOutput(false);

因此,如果您正在执行
,则不应尝试获取
输入流

好的,再深入一点,发现服务器出现403错误。这是一个安全错误,所以现在我的问题是如何克服它?提前谢谢。好的,我想好了。非常重要的是,如果你有同样的问题,不要使用这一行,也要检查你的URL。第一个问题是我的URL拼写错误,所以我得到了服务器的明显响应。在我修复我的URL后,它仍然有问题。最终是这一行:urlConnection.setDoOutput(true);显然,JAVA中的这一行强制http协议将GET更改为POST,而不考虑指定GET。仅供将来参考。谢谢你,这让我忙了一整天。谢谢,请回答你自己的问题@内森,你应该把这个作为你自己问题的答案,然后接受它。根据SO规则,这是完全好的,以前已经做过很多次了。太好了!现在不推荐使用真正有用的HttpStatus。此答案需要更新。请使用HttpURLConnection.HTTPStatus.BAD_请求,HTTPStatus.BAD_请求已从API级别22弃用。如何打印作为参数的内容?第[1608-1624]行的代码您的语句似乎不正确。默认情况下,setDoOutput为false。参考(Java文档):是的,您的回答是正确的,默认情况下为false。检查我的更新;)和+1,因为你变成了OP刚才作为评论提到的答案。
urlConnection.setDoOutput(false);