class DownloadFileAsync extends AsyncTask<String, String, String> { @Override
protected void onPreExecute() {
super.onPreExecute();
} @Override
protected String doInBackground(String... aurl) {
String filePaht="/sdcard/"; String fileName = aurl[0].substring(aurl[0].lastIndexOf("/") + 1,
aurl[0].length()); int count;
try { URL conurl = new URL(aurl[0]);
File file = new File(filePath + fileName); InputStream input = new BufferedInputStream(conurl.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024]; while ((count = input.read(data)) != -1) { output.write(data, 0, count);
}
output.flush();
output.close();
input.close(); } catch (Exception e) {
Log.e("error", e.getMessage().toString());
}
return null;
} @Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values); } @Override
protected void onPostExecute(String result) {
Toast.makeText(
PhotosActivity.this,
getResources().getString(R.string.downFileTip) + result,
Toast.LENGTH_SHORT).show();
} }我用这个方法可以下载图片但是不能下载文件,例如pdf文件
下载文件时InputStream input = new BufferedInputStream(conurl.openStream());抛出IO异常,通过网络调查 是因为得不到数据流,可是为什么图片的就可以,文件就不可以呢android 网络
protected void onPreExecute() {
super.onPreExecute();
} @Override
protected String doInBackground(String... aurl) {
String filePaht="/sdcard/"; String fileName = aurl[0].substring(aurl[0].lastIndexOf("/") + 1,
aurl[0].length()); int count;
try { URL conurl = new URL(aurl[0]);
File file = new File(filePath + fileName); InputStream input = new BufferedInputStream(conurl.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024]; while ((count = input.read(data)) != -1) { output.write(data, 0, count);
}
output.flush();
output.close();
input.close(); } catch (Exception e) {
Log.e("error", e.getMessage().toString());
}
return null;
} @Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values); } @Override
protected void onPostExecute(String result) {
Toast.makeText(
PhotosActivity.this,
getResources().getString(R.string.downFileTip) + result,
Toast.LENGTH_SHORT).show();
} }我用这个方法可以下载图片但是不能下载文件,例如pdf文件
下载文件时InputStream input = new BufferedInputStream(conurl.openStream());抛出IO异常,通过网络调查 是因为得不到数据流,可是为什么图片的就可以,文件就不可以呢android 网络
解决方案 »
- android java 获得root权限后,执行adb shell命令的问题
- 有人知道Android的MediaPlayer错误(-19,0)吗
- Listview 点击偶尔会错位
- 在android应用程序中使用执行linux命令
- android做界面有什么好的方法吗
- Intent怎么调用类中的方法?
- android下如何读取PDF文件?
- android logcat不断打印错误消息
- eclipse编译完成后,模拟器运行报错,(真机没问题,api版本和 模拟器版本一致)
- JSP页面显示不了图片
- 谁知道怎么去掉ANDROID 4.0的红色边框
- android TextView setMovementMethod 滑动 背景
conn.connect
conn.onpenInputStream