android 下载文件
16lz
2021-01-23
public void downFile(String url,String filePath)
throws IOException {
url="http://10.85.185.116:8088/myIIS/Nobody.mp4";
filePath="/sdcard/NoBody.mp4";
URL Url = new URL(url);
URLConnection conn = Url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
int fileSize = conn.getContentLength();// 根据响应获取文件大小
if (fileSize <= 0) { // 获取内容长度为0
throw new RuntimeException("无法获知文件大小 ");
}
if (is == null) { // 没有下载流
//sendMsg(Down_ERROR);
throw new RuntimeException("无法获取文件");
}
FileOutputStream FOS = new FileOutputStream(filePath); // 创建写入文件内存流,通过此流向目标写文件
byte buf[] = new byte[1024];
int downLoadFilePosition = 0;
int numread;
while ((numread = is.read(buf)) != -1) {
FOS.write(buf, 0, numread);
downLoadFilePosition += numread;
}
try {
is.close();
} catch (Exception ex) {}
}
throws IOException {
url="http://10.85.185.116:8088/myIIS/Nobody.mp4";
filePath="/sdcard/NoBody.mp4";
URL Url = new URL(url);
URLConnection conn = Url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
int fileSize = conn.getContentLength();// 根据响应获取文件大小
if (fileSize <= 0) { // 获取内容长度为0
throw new RuntimeException("无法获知文件大小 ");
}
if (is == null) { // 没有下载流
//sendMsg(Down_ERROR);
throw new RuntimeException("无法获取文件");
}
FileOutputStream FOS = new FileOutputStream(filePath); // 创建写入文件内存流,通过此流向目标写文件
byte buf[] = new byte[1024];
int downLoadFilePosition = 0;
int numread;
while ((numread = is.read(buf)) != -1) {
FOS.write(buf, 0, numread);
downLoadFilePosition += numread;
}
try {
is.close();
} catch (Exception ex) {}
}
更多相关文章
- Android中DownloadManager实现文件下载实例详解
- 【转】Android 文件系统的权限设置
- 安卓android readonly 无法增删文件
- android 使用statfs获得文件路径可用空间大小的方法
- android Uri利用及解析(文件操作)以及与路径的相互转换
- Android Studio 打不开layout 文件夹下的xml
- Android文件管理器与media数据库的同步问题
- 【Android Developers Training】 42. 从另一台设备接收文件