Android网络文件下载模块整理

一、知识基础

  tomcat服务器配置

  理解http协议

  理解javaIO操作相关知识

      SDcard操作知识

  Android 权限配置

二、实现步骤

  1、从网上获取资源

 1 public String download(String urlStr) {

 2         StringBuffer sb = new StringBuffer();

 3         String line = null;

 4         BufferedReader buffer = null;

 5         try {

 6             // 创建一个URL对象

 7             url = new URL(urlStr);

 8             // 创建一个Http连接

 9             HttpURLConnection urlConn = (HttpURLConnection) url

10                     .openConnection();

11             // 使用IO流读取数据

12             buffer = new BufferedReader(new InputStreamReader(urlConn

13                     .getInputStream()));

14             while ((line = buffer.readLine()) != null) {

15                 sb.append(line);

16             }

17         } catch (Exception e) {

18             e.printStackTrace();

19         } finally {

20             try {

21                 buffer.close();

22             } catch (Exception e) {

23                 e.printStackTrace();

24             }

25         }

26         return sb.toString();

27     }

28 

29     /**

30      * 该函数返回整形 -1:代表下载文件出错 0:代表下载文件成功 1:代表文件已经存在

31      */

32     public int downFile(String urlStr, String path, String fileName) {

33         InputStream inputStream = null;

34         try {

35             FileUtils fileUtils = new FileUtils();

36             

37             if (fileUtils.isFileExist(path + fileName)) {

38                 return 1;

39             } else {

40                 inputStream = getInputStreamFromUrl(urlStr);

41                 File resultFile = fileUtils.write2SDFromInput(path,fileName, inputStream);

42                 if (resultFile == null) {

43                     return -1;

44                 }

45             }

46         } catch (Exception e) {

47             e.printStackTrace();

48             return -1;

49         } finally {

50             try {

51                 inputStream.close();

52             } catch (Exception e) {

53                 e.printStackTrace();

54             }

55         }

56         return 0;

57     }

58 

59     /**

60      * 根据URL得到输入流

61      * 

62      * @param urlStr

63      * @return

64      * @throws MalformedURLException

65      * @throws IOException

66      */

67     public InputStream getInputStreamFromUrl(String urlStr)

68             throws MalformedURLException, IOException {

69         url = new URL(urlStr);

70         HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

71         InputStream inputStream = urlConn.getInputStream();

72         return inputStream;

73     }

74 }
从网络下载文件

 

  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老师,表示感谢

 

你可能感兴趣的:(android)