Android网络文件下载模块整理

一、知识基础

  tomcat服务器配置

  理解http协议

  理解javaIO操作相关知识

SDcard操作知识

  Android 权限配置

二、实现步骤

  1、从网上获取资源

 public String download(String urlStr) {
         StringBuffer sb = new StringBuffer();
         String line = null;
         BufferedReader buffer = null;
         try {
             // 创建一个URL对象
             url = new URL(urlStr);
             // 创建一个Http连接
             HttpURLConnection urlConn = (HttpURLConnection) url
                     .openConnection();
             // 使用IO流读取数据
             buffer = new BufferedReader(new InputStreamReader(urlConn
                     .getInputStream()));
             while ((line = buffer.readLine()) != null) {
                 sb.append(line);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             try {
                 buffer.close();
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
         return sb.toString();
     }

     /**
      * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在
      */
     public int downFile(String urlStr, String path, String fileName) {
         InputStream inputStream = null;
         try {
             FileUtils fileUtils = new FileUtils();

             if (fileUtils.isFileExist(path + fileName)) {
                 return 1;
             } else {
                 inputStream = getInputStreamFromUrl(urlStr);
                 File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream);
                 if (resultFile == null) {
                     return -1;
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
             return -1;
         } finally {
             try {
                 inputStream.close();
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
         return 0;
     }

     /**
      * 根据URL得到输入流
      *
      * @param urlStr
      * @return
      * @throws MalformedURLException
      * @throws IOException
      */
     public InputStream getInputStreamFromUrl(String urlStr)
             throws MalformedURLException, IOException {
         url = new URL(urlStr);
         HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
         InputStream inputStream = urlConn.getInputStream();
         return inputStream;
     }
 }

从网络下载文件

  2、将资源写入SDcard

public class FileUtils {
    private String SDPATH;

    public String getSDPATH() {
        return SDPATH;
    }
    public FileUtils() {
        //得到当前外部存储设备的目录
        // /SDCARD
        SDPATH = Environment.getExternalStorageDirectory() + "/";
    }
    /**
     * 在SD卡上创建文件
     *
     * @throws IOException
     */
    public File creatSDFile(String fileName) throws IOException {
        File file = new File(SDPATH + fileName);
        file.createNewFile();
        return file;
    }

    /**
     * 在SD卡上创建目录
     *
     * @param dirName
     */
    public File creatSDDir(String dirName) {
        File dir = new File(SDPATH + dirName);
        dir.mkdirs();
        return dir;
    }

    /**
     * 判断SD卡上的文件夹是否存在
     */
    public boolean isFileExist(String fileName){
        File file = new File(SDPATH + fileName);
        return file.exists();
    }

    /**
     * 将一个InputStream里面的数据写入到SD卡中
     */
    public File write2SDFromInput(String path,String fileName,InputStream input){
        File file = null;
        OutputStream output = null;
        try{
            creatSDDir(path);
            file = creatSDFile(path + fileName);
            output = new FileOutputStream(file);//写入数据
            byte buffer [] = new byte[4 * 1024];
            while((input.read(buffer)) != -1){
                output.write(buffer);
            }
            output.flush();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            try{
                output.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
        return file;
    }

}

SDCard 读写操作

三、版本说明

  程序源码来源Mars老师,表示感谢

上一篇:iOS 报错信息: dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from


下一篇:Python之路【第六篇】:Python迭代器、生成器、面向过程编程