简单的网络资源下载

2019-01-23 20:48:52

从网络中获取资源,并打开一个输出流:


1

URL url=new URL("www.baidu.com/image/1.jpg");

2

InputStream in=url.openStream();//返回一个输入流

获取输入流的数据,进行存储:

        1.获取数据资源长度。


1

        URLConnection con=url.openConnection();//打开该URL对应的URLConnection.

2

        long size=con.getContentLength();//获取连接URL资源的长度

        2.将数据存入文件中


1

byte[] buff=new byte[1024];//定义一个字节数组,用于装载数据

2

RandomAccessFile file=new RandomAccessFile("D:\\2.jpg","rw");//用于对随机访问文件写入数据

3

int i=0;

4

while(i!=-1){

5

     i=in.read(buff);//将输入流中数据存储到字节数组中,返回数组存储到的数据长度,当流中没有数据是,返回-1   

6

    if(i>=0){

7

            file.write(buff,0,i);//将数据写入文件中

8

    }

9

}

url网络资源下载完整方法


1

/**

2

     * url网络资源下载

3

     * @param name1:url网络资源链接

4

     * @param name2:将资源存储到的位置和名字

5

     */

6

    public void urlXZ(String name1,String name2) {

7

        URL url = null;

8

        try {

9

            System.out.println("解析网络资源链接...");

10

            url = new URL(name1);   //还没有开始获取资源,什么时候用的时候什么时候从网上获取资源

11

            System.out.println("网络资源链接解析完毕!");

12

        } catch (MalformedURLException e) {

13

            System.out.println("获取网络资源时异常");

14

            e.printStackTrace();

15

        }

16

        

17

        long lengthd = 0;

18

        try {

19

            System.out.println("正在获取网络资源长度...");

20

            lengthd = url.openConnection().getContentLength();

21

            System.out.println("数据长度获取完毕!:"+lengthd+"字节");

22

        } catch (IOException e) {

23

            System.out.println("获取文件长度时异常");

24

            e.printStackTrace();

25

        }

26

27

        InputStream in = null;

28

29

        try {

30

            System.out.println("正在将数据从网络上下载下来放入缓冲流中...");

31

            in = new BufferedInputStream(url.openStream()); //现在才开始从网络中下载数据

32

            System.out.println("数据缓冲完毕!");

33

        } catch (IOException e) {

34

            System.out.println("将网络数据放入缓冲流时异常");

35

            e.printStackTrace();

36

        }

37

38

39

        byte[] b = new byte[1024];

40

        FileOutputStream filea = null;

41

        try {

42

            filea = new FileOutputStream(name2);

43

        } catch (FileNotFoundException e1) {

44

            System.out.println("文件创建时异常");

45

            e1.printStackTrace();

46

        }

47

        System.out.println("正在将数据写入本地文件...");

48

        int i = 0;

49

        while (i >= 0) {

50

            try {

51

                i = in.read(b);

52

            } catch (IOException e) {

53

                System.out.println("数据流存入数组时异常");

54

                e.printStackTrace();

55

            }

56

            if (i >= 0) {

57

                try {

58

                    filea.write(b, 0, i);

59

                } catch (IOException e) {

60

                    System.out.println("文件写入时异常");

61

                    e.printStackTrace();

62

                } catch (IndexOutOfBoundsException e) {

63

                    System.out.println("数组越界异常");

64

                    e.printStackTrace();

65

                }

66

            }

67

68

        }

69

        System.out.println("文件下载完毕!");

70

        try {

71

            filea.close();

72

        } catch (IOException e) {

73

            System.out.println("调用filea.close()出错");

74

            e.printStackTrace();

75

        }

76

        try {

77

            in.close();

78

        } catch (IOException e) {

79

            System.out.println("调用in.close()");

80

            e.printStackTrace();

81

        }

82

83

    }



  • 2020-02-07 23:46:44

    You must call removeView() on the child's parent first解决办法

    出现这样的情况最多是发生在recyclerView中,holder复用的过程中,多次添加view,第一次添加的时候view有了父类了,可能就是复用引起的。 我是发生在给recyclerView添加广告view的时候发生的。

  • 2020-02-11 17:43:35

    基于VCamera,仿微信录制短视频

    vcamera.so这个确实挺好用,可定制性也挺高,但是确定也不小,需要引入的这个so包,10M啊。对于安装包苛刻的用户,这是致命啊。 我现在是抛弃他了。但是在这里还是记录一下用法吧。防止以后再用他。

  • 2020-02-13 13:37:53

    mysql随机排序

    首页热门栏目需要随机显示几条信息

  • 2020-02-14 20:08:17

    Android-应用被作为第三方浏览器打开

    微信里的文章页面,可以选择“在浏览器打开”。现在很多应用都内嵌了WebView,那是否可以使自己的应用作为第三方浏览器打开此文章呢?

  • 2020-02-16 16:17:15

    CocoaPods安装和使用教程

    当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等。可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其他类库又用到其他类库,“子子孙孙无穷尽也”,这也许是比较特殊的情况。总之小编的意思就是,手动一个个去下载所需类库十分麻烦。另外一种常见情况是,你项目中用到的类库有更新,你必须得重新下载新版本,重新加入到项目中,十分麻烦。如果能有什么工具能解决这些恼人的问题,那将“善莫大焉”。所以,你需要 CocoaPods。