Android 文件下载
来源:互联网 发布:淘宝双十一不能报名 编辑:程序博客网 时间:2024/05/31 19:38
文章分类:移动开发
1.使用HTTP协议下载文件
2.将下载的文件写入SDCARD
- package net.wl.utils;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import android.os.Environment;
- /**
- * 文件下载工具类
- *
- * @author wanglin
- *
- */
- public class FileUtils {
- private String SDPATH;
- public String getSDPATH() {
- return SDPATH;
- }
- public FileUtils() {
- // 得到当前外包存储设备的目录
- SDPATH = Environment.getExternalStorageDirectory() + "/";
- }
- /**
- * 在SD卡上创建文件
- *
- * @param fileName
- * @return
- * @throws IOException
- */
- public File createSDFile(String fileName) throws IOException {
- File file = new File(SDPATH + fileName);
- file.createNewFile();
- return file;
- }
- /**
- * 在SD卡上创建目录
- *
- * @param dirName
- * @return
- */
- public File createSDDir(String dirName) {
- File dir = new File(SDPATH + dirName);
- dir.mkdir();
- return dir;
- }
- /**
- * 判断文件是否存在
- *
- * @param fileName
- * @return
- */
- public boolean isFileExist(String fileName) {
- File file = new File(SDPATH + fileName);
- return file.exists();
- }
- /**
- * 将一个InputStream的数据写入到SD卡中
- *
- * @param path
- * @param fileName
- * @param input
- * @return
- */
- public File writeDataToSDCrard(String path, String fileName,
- InputStream input) {
- File file = null;
- OutputStream output = null;
- try {
- // 创建目录
- this.createSDDir(path);
- // 创建文件
- file = this.createSDFile(path + fileName);
- // 创建文件输出流
- output = new FileOutputStream(file);
- // 4个字节的输出
- byte[] b = new byte[4 * 1024];
- while ((input.read(b)) != -1) {
- output.write(b);
- }
- // 刷新流
- output.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- output.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return file;
- }
- }
- package net.wl.utils;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.content.Context;
- import android.widget.Toast;
- /**
- * HTTP下载类
- * @author wanglin
- *
- */
- public class HttpDownloader {
- private URL url = null;
- /**
- * 该函数返回整型: -1:代表出错 , 0:代表成功 , 1:代表已经存在
- *
- * @description 下载的通用方法
- * @param urlStr
- * @param path
- * @param fileName
- * @return
- */
- public int downFile(Context context, String urlStr, String path,
- String fileName) {
- InputStream inputStream = null;
- FileUtils fileUtils = new FileUtils();
- if (fileUtils.isFileExist(path + fileName)) {
- Toast.makeText(context, "文件已经存在", Toast.LENGTH_LONG).show();
- return 1;
- } else {
- try {
- inputStream = getInputSreamFromUrl(urlStr);
- File resultFile = fileUtils.writeDataToSDCrard(path, fileName,
- inputStream);
- if (resultFile == null) {
- Toast.makeText(context, "下载失败", Toast.LENGTH_LONG).show();
- return -1;
- }
- } catch (IOException e) {
- e.printStackTrace();
- return -1;
- } finally {
- try {
- inputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- return -1;
- }
- }
- }
- Toast.makeText(context, "下载成功", Toast.LENGTH_LONG).show();
- return 0;
- }
- /**
- * 得到输入流
- *
- * @param urlstr
- * @return
- * @throws IOException
- */
- public InputStream getInputSreamFromUrl(String urlstr) throws IOException {
- url = new URL(urlstr);
- HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
- InputStream is = urlconn.getInputStream();
- return is;
- }
- }
- Android 文件下载
- Android 文件下载
- android文件下载!download!
- android 文件下载
- android:文件下载
- Android 多线程文件下载
- android 文件的下载
- Android文件下载 HttpURLConnection
- 文件下载(java) android
- android 之文件下载
- android文件下载!download!
- Android文件下载
- Android之文件下载
- Android之文件下载
- android apk文件下载
- Android中的文件下载
- android 下载文件类
- Android开发 --- 文件下载
- 带解释的VBA短句
- comboBox 的DropDownStyle 要设置为“dropdownList”
- 项目百态(第19届Jolt奖获奖作品,入木三分刻画软件项目众生图)
- undefined reference to typeinfo - C++ error message
- 兄弟连原创视频光盘火热销售
- Android 文件下载
- Linux 时间日期函数
- 励志
- linux Eclipse 使用数学库 配置方法
- js跨域请求数据实现
- uImage与zImage的区别
- SAP中的Currency Converting Factor
- 这个很好
- hibernate对实体外键查询理解