2014-06-08 124 views
0

正在嘗試從服務器下載文件,但沒有成功,我的代碼似乎是確定的Android的Java無法下載文件

URL url = null; 
URLConnection con = null; 
    int i; 
    try { 
url = new URL(downlink); // url : http://10.0.2.2:800/myproject/down/file9.txt 
con = url.openConnection(); 
String dest_path = c.getFilesDir().getPath() + "/textfile.txt"; //Download Location set to : /data/data/com.myproject.androidt/files/textfile.txt 
File file = new File(dest_path); 
    BufferedInputStream bis = new BufferedInputStream(con.getInputStream()); 
    FileOutputStream fos = context.openFileOutput("textfile.txt", Context.MODE_PRIVATE); 
    BufferedOutputStream bos = new BufferedOutputStream(fos); 
    while ((i = bis.read()) != -1) { 
    bos.write(i); 
     } 
    bos.flush(); 
    bis.close(); 
    return true; 
    } catch (MalformedInputException malformedInputException) { 
    Log.d("dark","Failure : MalformedInputException occured in downloading"); 
      // error in download 
    return false; 
    } catch (IOException ioException) { 
    Log.d("dark","Failure : IO Error occured in downloading"); 
    return false; 
     // error in download 
    } 

,請幫助我得到IO異常,不知道有什麼錯碼:(

+0

哪一行是拋出異常 – todd

+0

@todd這一行拋出異常:BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName())); – user134929

回答

0

我相信你必須調用openFileOutput方法來獲取FileOutputStream

FileOutputStream fos = openFileOutput("textfile.txt", Context.MODE_PRIVATE); 

,你只需要在文件名而不是路徑。

+0

謝謝你救了我的一天:)現在它的工作.....我不需要完整的路徑,問題是我通過完整路徑 – user134929

+0

我編輯的代碼,以便可以幫助某人:) – user134929

+0

你知道如何刪除Android調試監視器中的應用程序文件夾,我不能刪除我在我的應用程序數據文件夾中創建的文件夾 – user134929