正在嘗試從服務器下載文件,但沒有成功,我的代碼似乎是確定的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異常,不知道有什麼錯碼:(
哪一行是拋出異常 – todd
@todd這一行拋出異常:BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName())); – user134929