简单的网络资源下载

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

    }



  • 2017-02-16 08:09:01

    HTML中PRE和p的区别

    pre 元素可定义预格式化的文本。被包围在 pre 元素中的文本通常会保留空格和换行符。而文本也会呈现为等宽字体。 <pre> 标签的一个常见应用就是用来表示计算机的源代码。

  • 2017-02-16 15:14:14

    动态加载js和css

    开发过程中经常需要动态加载js和css,今天特意总结了一下常用的方法。

  • 2017-02-17 17:11:24

    mysql插入数据后返回自增ID的方法c

    产生的ID 每次连接后保存在服务器中。这意味着函数向一个给定客户端返回的值是该客户端产生对影响AUTO_INCREMENT列的最新语句第一个 AUTO_INCREMENT值的。这个值不能被其它客户端影响,即使它们产生它们自己的 AUTO_INCREMENT值。这个行为保证了你能够找回自己的 ID 而不用担心其它客户端的活动,而且不需要加锁或处理

  • 2017-02-21 07:59:45

    不会被 iOS 停掉的网页定时器

    其实这个标题略微有点标题党:iOS 中,除了少数服务(如播放音乐),大部分 App 在用户按了 Home 键之后,过不了多久就会被完全冻结,这对 Safari 同样适用。本文不考虑这样情况,只考虑 Safari 运行时,怎样让定时器持续工作。

  • 2017-02-24 15:04:10

    PHP 中的 9 个魔术方法

    这个标题有点牵强因为php有不只9种魔术方法, 但是这些将会引导你使用php魔术方法一个好的开始。它可能魔幻,但是并不需要魔杖。 这些'魔术'方法拥有者特殊的名字,以两个下划线开始,表示这些方法在php特定事件下将会被触发。这可能听起来有点自动魔法但是它真的很酷的,我们已经看过一个简单的例子在 last post,即我们使用一个构造器-使用这个作为我们第一个例子