0

我正在尝试在我的应用程序中下载图像文件,但我不断收到“java.io.FileNotFoundException”,即使我可以在浏览器上查看图像。这是假设下载文件的代码。您可以在浏览器上测试链接

new Thread(new Runnable() {

            @Override
            public void run() {

                String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
                String sample = "https://yande.re/image/617a7bff242f2ec56ccfc84df53604fc/yande.re%20263544%20chibi%20shinomiya_himawari%20vividred_operation.jpg";
                try
                {   
                  URL url = new URL(sample);
                  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                  urlConnection.setRequestMethod("POST");
                  urlConnection.setDoOutput(true);                   
                  urlConnection.connect();                  


                  File SDCardRoot = new File(Environment.getExternalStorageDirectory(),"Yandere/Wallpapers");
                  SDCardRoot.mkdirs();
                  String filename="downloadedFile.jpg";   
                  Log.i("Local filename:",""+filename);
                  File file = new File(SDCardRoot,filename);
                  if(file.createNewFile())
                  {
                    file.createNewFile();
                  }                 
                  FileOutputStream fileOutput = new FileOutputStream(file);
                  InputStream inputStream = urlConnection.getInputStream();
                  int totalSize = urlConnection.getContentLength();
                  int downloadedSize = 0;   
                  byte[] buffer = new byte[1024];
                  int bufferLength = 0;
                  while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
                  {                 
                    fileOutput.write(buffer, 0, bufferLength);                  
                    downloadedSize += bufferLength;                 
                    Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
                  }             
                  fileOutput.close();
                  if(downloadedSize==totalSize) filepath=file.getPath();    
                } 
                catch (MalformedURLException e) 
                {
                  e.printStackTrace();
                } 
                catch (IOException e)
                {
                  filepath=null;
                  e.printStackTrace();
                }}
        }).start();

错误:

 08-05 16:36:28.032: W/System.err(17064): java.io.FileNotFoundException: https://yande.re/image/617a7bff242f2ec56ccfc84df53604fc/yande.re%20263544%20chibi%20shinomiya_himawari%20vividred_operation.jpg
08-05 16:36:28.032: W/System.err(17064):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
08-05 16:36:28.032: W/System.err(17064):    at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
08-05 16:36:28.032: W/System.err(17064):    at angel.util.Downloader$1.run(Downloader.java:52)
08-05 16:36:28.032: W/System.err(17064):    at java.lang.Thread.run(Thread.java:856)
4

3 回答 3

1

将您的 Url 字符串从:

https://yande.re/image/617a7bff242f2ec56ccfc84df53604fc/yande.re%20263544%20chibi%20shinomiya_himawari%20vividred_operation.jpg

http://yande.re/image/617a7bff242f2ec56ccfc84df53604fc/yande.re%20263544%20chibi%20shinomiya_himawari%20vividred_operation.jpg

实际上将“https”更改为“http”,它应该可以工作。这种变化对我有用。

于 2013-08-05T08:44:16.143 回答
0

您正在POST代码中访问该 URL,而不是访问GET它。这导致服务器返回405 Method Not Allowed错误。如果您urlConnection.setRequestMethod("GET");用作 Shamim Ahmmed 建议,那么您应该能够获得您想要的文件。

您可以在此处阅读有关不同请求方法的更多信息。基本上,POST通常在您想向服务器发送数据时GET使用,并且在您只是尝试从服务器获取数据时使用。在图像的情况下,服务器未配置为能够处理POST对它的请求,只是返回一个 HTML 错误页面而不是您期望的图像。

于 2013-08-05T08:18:17.350 回答
0

您应该使用以下 HTTP GET 请求方法。您的 Web 服务器似乎不支持 HTTP POST。

 urlConnection.setRequestMethod("GET");
于 2013-08-05T08:11:47.093 回答