简单的网络资源下载

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-12-17 11:58:55

    FFmpeg文章目录

    seek ffmpeg # How to seek in mp4/mkv/ts/flv ffmpeg # flags &= ~AVSEEK_FLAG_BACKWARD ffmpeg # AVSEEK_FLAG concat ffmpeg # concat 连接两个视频 ffmpeg # -f concat -i mylist.txt ffmpeg # concat详解+音画同步策略 截图

  • 2019-12-18 23:26:00

    FFMPEG命令记录

    ffmpeg,拼接两个音频,剪切音频片段,多个音频混音,剪切一段MP4并转换成gif,改变音量大小,音频淡入淡出,音频格式处理

  • 2019-12-19 00:04:44

    ffmpeg concat video and mix audio

    在ffmpeg中,官网给出两种连接媒体文件(音频、视频、etc..)的解决方案。 the concat "demuxer" the concat "protocol" 对比而言, demuxer更加灵活一些,需要媒体文件是属于相同的编解码器,但是可以属于不同的容器格式(mp3,wav, mp4, mov, etc..). 而protocol只适用于少数集中容器格式。

  • 2019-12-19 00:16:30

    android采用FFmpeg实现音频混合与拼接剪切

    接触FFmpeg有一段时间了,它是音视频开发的开源库,几乎其他所有播放器、直播平台都基于FFmpeg进行二次开发。本篇文章来总结下采用FFmpeg进行音频处理:音频混合、音频剪切、音频拼接与音频转码。

  • 2019-12-19 15:01:58

    spring boot 在Linux下服务启动报错Unable to find Java

    将 Spring boot 安装为 Linux 服务启动,后输入 service myapp start 报错 Unable to find Java ,但是使用 java -jar myapp.jar 启动成功。不知道为啥引起的,经过百度找到下面这个解决方法和我的情况一样,终于把问题解决