2015-08-27 125 views
1

我無法使用基本http-auth下載文件。我有auth字符串授權,但我得到異常:FileNotFound。 代碼:通過http-auth下載文件

URL url = new URL(params[0]); 
       URLConnection conection = url.openConnection(); 

       conection.setRequestProperty("Authorization", authString); 
       conection.setRequestMethod("GET"); 
       conection.setAllowUserInteraction(false); 
       conection.setDoInput(true); 
       conection.setDoOutput(true); 
       conection.connect(); 



       int lenghtOfFile = conection.getContentLength(); 


       // Exception on line below 
       BufferedInputStream input = new BufferedInputStream(conection.getInputStream()); 

       // Output stream 
       OutputStream output = new FileOutputStream(filePath); 

       byte data[] = new byte[1024]; 
... 

lenghtOfFile不爲0。此外,我嘗試使用HtppClient此任務,但得到的異常了。

+0

刪除setDoOutput(true)行。 – BNK

+1

謝謝,這對我有幫助) –

回答

1

正如我所評論的,您應該刪除setDoOutput(true)行,因爲這是GET請求。