Android 文件下载

来源:互联网 发布:淘宝双十一不能报名 编辑:程序博客网 时间:2024/05/31 19:38

文章分类:移动开发

1.使用HTTP协议下载文件

2.将下载的文件写入SDCARD

 

Java代码 复制代码 收藏代码
  1. package net.wl.utils;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileOutputStream;   
  5. import java.io.IOException;   
  6. import java.io.InputStream;   
  7. import java.io.OutputStream;   
  8.   
  9. import android.os.Environment;   
  10.   
  11. /**  
  12.  * 文件下载工具类  
  13.  *   
  14.  * @author wanglin  
  15.  *   
  16.  */  
  17. public class FileUtils {   
  18.     private String SDPATH;   
  19.   
  20.     public String getSDPATH() {   
  21.         return SDPATH;   
  22.     }   
  23.   
  24.     public FileUtils() {   
  25.         // 得到当前外包存储设备的目录   
  26.         SDPATH = Environment.getExternalStorageDirectory() + "/";   
  27.     }   
  28.   
  29.     /**  
  30.      * 在SD卡上创建文件  
  31.      *   
  32.      * @param fileName  
  33.      * @return  
  34.      * @throws IOException  
  35.      */  
  36.     public File createSDFile(String fileName) throws IOException {   
  37.         File file = new File(SDPATH + fileName);   
  38.         file.createNewFile();   
  39.         return file;   
  40.     }   
  41.   
  42.     /**  
  43.      * 在SD卡上创建目录  
  44.      *   
  45.      * @param dirName  
  46.      * @return  
  47.      */  
  48.     public File createSDDir(String dirName) {   
  49.         File dir = new File(SDPATH + dirName);   
  50.         dir.mkdir();   
  51.         return dir;   
  52.     }   
  53.   
  54.     /**  
  55.      * 判断文件是否存在  
  56.      *   
  57.      * @param fileName  
  58.      * @return  
  59.      */  
  60.     public boolean isFileExist(String fileName) {   
  61.         File file = new File(SDPATH + fileName);   
  62.         return file.exists();   
  63.     }   
  64.   
  65.     /**  
  66.      * 将一个InputStream的数据写入到SD卡中  
  67.      *   
  68.      * @param path  
  69.      * @param fileName  
  70.      * @param input  
  71.      * @return  
  72.      */  
  73.     public File writeDataToSDCrard(String path, String fileName,   
  74.             InputStream input) {   
  75.         File file = null;   
  76.         OutputStream output = null;   
  77.   
  78.         try {   
  79.             // 创建目录   
  80.             this.createSDDir(path);   
  81.             // 创建文件   
  82.             file = this.createSDFile(path + fileName);   
  83.             // 创建文件输出流   
  84.             output = new FileOutputStream(file);   
  85.             // 4个字节的输出   
  86.             byte[] b = new byte[4 * 1024];   
  87.             while ((input.read(b)) != -1) {   
  88.                 output.write(b);   
  89.             }   
  90.             // 刷新流   
  91.             output.flush();   
  92.   
  93.         } catch (IOException e) {   
  94.             e.printStackTrace();   
  95.         } finally {   
  96.             try {   
  97.                 output.close();   
  98.             } catch (IOException e) {   
  99.                 e.printStackTrace();   
  100.             }   
  101.         }   
  102.   
  103.         return file;   
  104.     }   
  105. }  

 

 

 

Java代码 复制代码 收藏代码
  1. package net.wl.utils;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5. import java.io.InputStream;   
  6. import java.net.HttpURLConnection;   
  7. import java.net.URL;   
  8.   
  9. import android.content.Context;   
  10. import android.widget.Toast;   
  11.   
  12. /**  
  13.  * HTTP下载类  
  14.  * @author wanglin  
  15.  *  
  16.  */  
  17. public class HttpDownloader {   
  18.     private URL url = null;   
  19.   
  20.     /**  
  21.      * 该函数返回整型: -1:代表出错 , 0:代表成功 , 1:代表已经存在  
  22.      *   
  23.      * @description 下载的通用方法  
  24.      * @param urlStr  
  25.      * @param path  
  26.      * @param fileName  
  27.      * @return  
  28.      */  
  29.     public int downFile(Context context, String urlStr, String path,   
  30.             String fileName) {   
  31.         InputStream inputStream = null;   
  32.         FileUtils fileUtils = new FileUtils();   
  33.         if (fileUtils.isFileExist(path + fileName)) {   
  34.             Toast.makeText(context, "文件已经存在", Toast.LENGTH_LONG).show();   
  35.             return 1;   
  36.         } else {   
  37.             try {   
  38.                 inputStream = getInputSreamFromUrl(urlStr);   
  39.                 File resultFile = fileUtils.writeDataToSDCrard(path, fileName,   
  40.                         inputStream);   
  41.                 if (resultFile == null) {   
  42.                     Toast.makeText(context, "下载失败", Toast.LENGTH_LONG).show();   
  43.                     return -1;   
  44.                 }   
  45.   
  46.             } catch (IOException e) {   
  47.                 e.printStackTrace();   
  48.                 return -1;   
  49.             } finally {   
  50.                 try {   
  51.                     inputStream.close();   
  52.                 } catch (IOException e) {   
  53.                     e.printStackTrace();   
  54.                     return -1;   
  55.                 }   
  56.             }   
  57.         }   
  58.   
  59.         Toast.makeText(context, "下载成功", Toast.LENGTH_LONG).show();   
  60.         return 0;   
  61.   
  62.     }   
  63.   
  64.     /**  
  65.      * 得到输入流  
  66.      *   
  67.      * @param urlstr  
  68.      * @return  
  69.      * @throws IOException  
  70.      */  
  71.     public InputStream getInputSreamFromUrl(String urlstr) throws IOException {   
  72.         url = new URL(urlstr);   
  73.         HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();   
  74.         InputStream is = urlconn.getInputStream();   
  75.         return is;   
  76.     }   
  77. }  
原创粉丝点击