2013-08-05 37 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