简单的网络资源下载

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-03-28 09:27:41

    Java中Arrays的asList()方法

    Java中Arrays的asList()方法 可以将 数组转为List 但是,这个数组类型必须是 引用类型的,如果是8中基本数据类型就不可以 原因如下,引用别人的一篇文章:

  • 2017-03-28 10:58:01

    No such property: sonatypeRepo for class:

    这种问题一般是出现在导入一些开源项目的时候。原因为该项目的原作者会把项目发布到maven中央仓库中,所以在gradle中添加了相关的maven发布任务,而发布任务需要配置

  • 2017-04-02 00:42:51

    PHP的pm、pm.max_requests、memory_limit参数优化说明

    pm是来控制php-fpm的工作进程数到底是一次性产生固定不变(static)还是在运行过程中随着需要动态变化(dynamic)。众所周知,工作进程数与服务器性能息息相关,太少则不能及时处理请求,太多则会占用内存过大而拖慢系统。

  • 2017-04-02 00:44:46

    NGINX + PHP-FPM 502 相关事

    NGINX + PHP-FPM 报 502 错误,我想大部分 SA 都遇到过吧。 根据报错的频率,可以分为两种情况,间歇性的502和连续性的502。 这里只讨论第一种情况——间歇性的502。

  • 2017-04-02 00:52:26

    php-fpm占用系统资源分析

    由上图分析,可以看出共有602个进程,其中有601个进程休眠了。这好像有点不对劲,内核进程也就80个左右,加上memcached, nginx, mysqld,也不会超出90个。除了这些,剩下的只有php-fpm管理的php-cgi,难道是…?

  • 2017-04-02 00:56:36

    php-fpm占用系统资源分析

    由上图分析,可以看出共有602个进程,其中有601个进程休眠了。这好像有点不对劲,内核进程也就80个左右,加上memcached, nginx, mysqld,也不会超出90个。除了这些,剩下的只有php-fpm管理的php-cgi,难道是…?

  • 2017-04-03 14:23:17

    Android Studio --“Cannot resolve symbol” 解决办法

    Android Studio 无法识别同一个 package 里的其他类,将其显示为红色,但是 compile 没有问题。鼠标放上去后显示 “Cannot resolve symbol XXX”,重启 Android Studio,重新 sync gradle,Clean build 都没有用。