简单的网络资源下载

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

    }



  • 2019-07-11 11:35:20

    VirtualBox添加新硬盘

    昨晚在自己的virtualbox中的linux装matlab2010a,没想到硬盘空间不足,所以找了下怎么添加硬盘的资料。也很简单,每几条命令。 大致流程:我的virtualbox版本是3.2.8,linux版本是xubuntu10.01吧貌似

  • 2019-07-23 14:49:40

    Windows10上使用Linux子系统(WSL)

    Linux的Windows子系统让开发人员可以直接在Windows上运行Linux环境(包括大多数命令行工具,实用程序和应用程序),而无需建立在虚拟机的开销之上,整个系统共200多M,但包含了你能用到的所有功能,并且和windows完美互操作(省去linux挂载本地windows分区或目录的操作),目前Linux的windows子系统已经相当完善,可当作完整linux系统使用

  • 2019-07-24 01:21:15

    android开发无障碍app

    最近做一些为盲人提供服务的APP,还是挺有感触的,感谢手机和互联网的普及,他们的生活比以前丰富了很多。 通过读屏软件,盲人可以操作手机,上网浏览信息。读屏软件的工作原理很简单,就是读出屏幕上按钮、文本的信息。