2013-08-05 38 views
0

我尝试下载我的应用程序内的图像文件进行查看,但我不断收到“java.io.FileNotFoundException”即使我可以查看我的形象浏览器。这是假设下载文件的代码。你可以测试链路上的浏览器安卓: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) 
+0

发表您的堆栈跟踪,请 – Enigma

+0

如果更改HTTPS为纯老的HTTP,会发生什么? – iaindownie

+0

加了我的logcat – Tsukihara

回答

1
+0

更改为“HTTP”,实际上绕过了错误,但下载的文件是空的.. – Tsukihara

+0

正如有人说,改变POST到GET .. – Sushil

+0

对于我来说,我越来越184KB的下载。只需删除并重新连接您的设备,以使mediascanner运行。或者可以重新启动手机,然后再检查。 – Sushil

0

你应该使用下面的HTTP GET请求方法。看起来你的Web服务器不支持HTTP POST。

urlConnection.setRequestMethod("GET"); 
+0

使用“GET”已经尝试过“GET”还是同样的错误... – Tsukihara

0

您是POST荷兰国际集团到URL中的代码,而不是GET荷兰国际集团它。 这会导致服务器返回405 Method Not Allowed错误。如果你使用Shamim Ahmmed作为urlConnection.setRequestMethod("GET");,那么你应该能够得到你想要的文件。

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

+0

我已经尝试过。还是同样的错误... – Tsukihara