JAVA SE.docx

上传人:b****8 文档编号:9704575 上传时间:2023-05-20 格式:DOCX 页数:53 大小:257.77KB
下载 相关 举报
JAVA SE.docx_第1页
第1页 / 共53页
JAVA SE.docx_第2页
第2页 / 共53页
JAVA SE.docx_第3页
第3页 / 共53页
JAVA SE.docx_第4页
第4页 / 共53页
JAVA SE.docx_第5页
第5页 / 共53页
JAVA SE.docx_第6页
第6页 / 共53页
JAVA SE.docx_第7页
第7页 / 共53页
JAVA SE.docx_第8页
第8页 / 共53页
JAVA SE.docx_第9页
第9页 / 共53页
JAVA SE.docx_第10页
第10页 / 共53页
JAVA SE.docx_第11页
第11页 / 共53页
JAVA SE.docx_第12页
第12页 / 共53页
JAVA SE.docx_第13页
第13页 / 共53页
JAVA SE.docx_第14页
第14页 / 共53页
JAVA SE.docx_第15页
第15页 / 共53页
JAVA SE.docx_第16页
第16页 / 共53页
JAVA SE.docx_第17页
第17页 / 共53页
JAVA SE.docx_第18页
第18页 / 共53页
JAVA SE.docx_第19页
第19页 / 共53页
JAVA SE.docx_第20页
第20页 / 共53页
亲,该文档总共53页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

JAVA SE.docx

《JAVA SE.docx》由会员分享,可在线阅读,更多相关《JAVA SE.docx(53页珍藏版)》请在冰点文库上搜索。

JAVA SE.docx

JAVASE

JavaSE02Day03

Top

1.断点续传下载工具——DownloadInfo类编写以及TestHttpDownload的testHttpDownload1方法

2.断点续传下载工具——DownloadInfo序列化

3.断点续传下载工具——DownloadInfo的fetchFileSize方法和DownloadThread类

4.断点续传下载工具——完成DownloadFrame的UI及DownloadItem内部类

1断点续传下载工具——DownloadInfo类编写以及TestHttpDownload的testHttpDownload1方法

1.1问题

如何使用Java类表示要下载的文件的信息;下载网络上的真实存在的文件来测试Java连接网络的方式。

下面介绍断点续传下载工具的功能和界面效果。

断点续传下载工具的主要功能是实现同时下载多个文件,下载的过程中可以临时终止下载,可以随时继续下载。

程序启动后,显示的界面如图-1所示。

图-1

点击“选择”按钮,弹出输入下载地址的输入框,如图-2和图-3所示。

图-2

图-3

输入下载地址后,点击“确定”,弹出输出保存文件路径的输入框,如图-4和图-5所示。

图-4

图-5

点击“确定”后,开始下载,进度条变化,按钮变成“终止”,如图-6所示。

图-6

点击“终止”按钮,按钮上变成“开始”,如图-7所示。

图-7

点击“开始“按钮,继续下载,如图-8。

图-8

可以多个要下载的文件,同时下载,如图-9所示。

图-9

1.2方案

定义DownloadInfo类表示要下载的文件的信息,包括属性有:

要下载的文件的网络地址(url),下载到文件的位置(pos),文件的大小(fileSize),文件名(fileName),并定义这些属性的getter和setter方法;定义参数为url和fileName的构造方法;定义toString方法。

1.    privateStringurl;

2.    privatelongpos;

3.    privatelongfileSize;

4.    privateStringfileName;

创建TestHttpDownload类测试DownloadInfo的正确性。

取真实的网络下载地址,下载文件,设置下载文件位置的变化。

设置文件下载信息url和fileName。

1.DownloadInfoinfo=newDownloadInfo(

2."

连接网络,获取输入流。

1.URLurl=newURL(info.getUrl());

2.HttpURLConnectioncon=(HttpURLConnection)url.openConnection();

3.InputStreamis=con.getInputStream();

从输入流中读取信息,写入文件,在读取的过程中可以设定读取到的位置。

1.    RandomAccessFileraf=newRandomAccessFile(info.getFileName(),"rw");

2.    raf.seek(info.getPos());

3.    byte[]buffer=newbyte[BUFFER_SIZE];

4.    intb=-1;

5.    while((b=is.read(buffer))!

=-1){

6.        raf.write(buffer,0,b);

7.        info.setPos(info.getPos()+b);

8.        System.out.println(info.getPos());

9.    }

1.3实现

系统代码实现如下:

1.publicclassDownloadInfo{

2.    privateStringurl;

3.    privatelongpos;

4.    privatelongfileSize;

5.    privateStringfileName;

6.

7.    publicDownloadInfo(Stringurl,StringfileName){

8.        this.url=url;

9.        this.fileName=fileName;

10.    }

11.    publicStringgetUrl(){

12.        returnurl;

13.    }

14.    publicvoidsetUrl(Stringurl){

15.        this.url=url;

16.    }

17.    publiclonggetPos(){

18.        returnpos;

19.    }

20.    publicvoidsetPos(longpos){

21.        this.pos=pos;

22.    }

23.    publicStringgetFileName(){

24.        returnfileName;

25.    }

26.    publicvoidsetFileName(StringfileName){

27.        this.fileName=fileName;

28.    }

29.    publiclonggetFileSize(){

30.        returnfileSize;

31.    }

32.    publicvoidsetFileSize(longfileSize){

33.        this.fileSize=fileSize;

34.    }

35.}

TestHttpDownload类的testHttpDownload1方法的代码如下:

1.    @Test

2.    publicvoidtestHttpDownload1(){

3.        intBUFFER_SIZE=1024*8;

4.        try{

5.            DownloadInfoinfo=new

6.DownloadInfo("

7.v7.0.37/bin/apache-tomcat-7.0.37.zip","d:

\\tomcat.zip");

8.            URLurl=newURL(info.getUrl());

9.            HttpURLConnectioncon=(HttpURLConnection)url.openConnection();

10.            Stringprop="bytes="+info.getPos()+"-";

11.            System.out.println(prop);

12.            con.setRequestProperty("RANGE",prop);

13.            InputStreamis=con.getInputStream();

14.            RandomAccessFileraf=

15.newRandomAccessFile(info.getFileName(),"rw");

16.            raf.seek(info.getPos());

17.            byte[]buffer=newbyte[BUFFER_SIZE];

18.            intb=-1;

19.            while((b=is.read(buffer))!

=-1){

20.                raf.write(buffer,0,b);

21.                info.setPos(info.getPos()+b);

22.                System.out.println(info.getPos());

23.            }

24.        }catch(Exceptione){

25.            e.printStackTrace();

26.            TestCase.fail();

27.        }

28.    }

1.4扩展

测试Java连接网络的方式。

在TestHttpDownload类中添加testHttpDownload2方法,测试Java连接网络。

1.@Test

2.    publicvoidtestHttpDownload2(){

3.        intBUFFER_SIZE=1024*8;

4.        try{

5.            DownloadInfoinfo=new

6.DownloadInfo("

7.v7.0.37/bin/apache-tomcat-7.0.37.zip","d:

\\tomcat.zip");

8.            URLurl=newURL(info.getUrl());

9.            HttpURLConnectioncon=(HttpURLConnection)url.openConnection();

10.            Stringprop="bytes="+info.getFileSize()/2+"-";

11.            System.out.println(prop);

12.            con.setRequestProperty("RANGE",prop);

13.            InputStreamis=con.getInputStream();

14.            RandomAccessFileraf=

15.newRandomAccessFile(info.getFileName(),"rw");

16.            raf.seek(info.getPos());

17.            byte[]buffer=newbyte[BUFFER_SIZE];

18.            intb=-1;

19.            while((b=is.read(buffer))!

=-1){

20.                raf.write(buffer,0,b);

21.                info.setPos(info.getPos()+b);

22.                System.out.println(info.getPos());

23.            }

24.        }catch(Exceptione){

25.            e.printStackTrace();

26.            TestCase.fail();

27.        }

28.    }

TestHttpDownload类的完整代码如下所示:

1.publicclassTestHttpDownload{

2.    @Test

3.    publicvoidtestHttpDownload1(){

4.        intBUFFER_SIZE=1024*8;

5.        try{

6.            DownloadInfoinfo=newDownloadInfo(

7."

8.v7.0.37/bin/apache-tomcat-7.0.37.zip","d:

\\tomcat.zip");

9.            URLurl=newURL(info.getUrl());

10.            HttpURLConnectioncon=(HttpURLConnection)url.openConnection();

11.            Stringprop="bytes="+info.getPos()+"-";

12.            System.out.println(prop);

13.            con.setRequestProperty("RANGE",prop);

14.            InputStreamis=con.getInputStream();

15.            RandomAccessFileraf=

16.newRandomAccessFile(info.getFileName(),"rw");

17.            raf.seek(info.getPos());

18.            byte[]buffer=newbyte[BUFFER_SIZE];

19.            intb=-1;

20.            while((b=is.read(buffer))!

=-1){

21.                raf.write(buffer,0,b);

22.                info.setPos(info.getPos()+b);

23.                System.out.println(info.getPos());

24.            }

25.        }catch(Exceptione){

26.            e.printStackTrace();

27.            TestCase.fail();

28.        }

29.    }

30.

31.    @Test

32.    publicvoidtestHttpDownload2(){

33.        intBUFFER_SIZE=1024*8;

34.        try{

35.            DownloadInfoinfo=newDownloadInfo(

36."

37.v7.0.37/bin/apache-tomcat-7.0.37.zip","d:

\\tomcat.zip");

38.            URLurl=newURL(info.getUrl());

39.            HttpURLConnectioncon=(HttpURLConnection)url.openConnection();

40.            Stringprop="bytes="+info.getFileSize()/2+"-";

41.            System.out.println(prop);

42.            con.setRequestProperty("RANGE",prop);

43.            InputStreamis=con.getInputStream();

44.            RandomAccessFileraf=

45.newRandomAccessFile(info.getFileName(),"rw");

46.            raf.seek(info.getPos());

47.            byte[]buffer=newbyte[BUFFER_SIZE];

48.            intb=-1;

49.            while((b=is.read(buffer))!

=-1){

50.                raf.write(buffer,0,b);

51.                info.setPos(info.getPos()+b);

52.                System.out.println(info.getPos());

53.            }

54.        }catch(Exceptione){

55.            e.printStackTrace();

56.            TestCase.fail();

57.        }

58.    }

59.}

2断点续传下载工具——DownloadInfo序列化

2.1问题

使DownloadInfo类的对象可序列化,并测试是否能将DownloadInfo类的对象序列化到本地文件,并从本地文件反序列化出DownloadInfo类的对象。

2.2方案

使DownloadInfo类实现Serializable接口,使DownloadInfo类的对象可序列化。

1.publicclassDownloadInfoimplementsSerializable{}

创建TestDownloadInfo类,添加testSerializeObj方法,使用ObjectOutputStream类的writeObject方法将DownloadInfo类的对象序列化到文件。

1.ObjectOutputStreamoos=new

2.ObjectOutputStream(newFileOutputStream(tmpFile));

3.oos.writeObject(info);

添加testDeserializeObj方法,使用ObjectInputStream类的readObject方法,反序列化出DownloadInfo类的对象。

1.ObjectInputStreamois=newObjectInputStream(newFileInputStream(tmpFile));

2.DownloadInfoinfo=(DownloadInfo)ois.readObject();

2.3实现

系统代码实现如下:

DownloadInfo类代码如下:

1.publicclassDownloadInfoimplementsSerializable{

2.    privatestaticfinallongserialVersionUID=1L;

3.    privateStringurl;

4.    privatelongpos;

5.    privatelongfileSize;

6.    privateStringfileName;

7.    publicDownloadInfo(Stringurl,StringfileName){

8.        this.url=url;

9.        this.fileName=fileName;

10.    }

11.    publicStringgetUrl(){

12.        returnurl;

13.    }

14.    publicvoidsetUrl(Stringurl){

15.        this.url=url;

16.    }

17.    publiclonggetPos(){

18.        returnpos;

19.    }

20.    publicvoidsetPos(longpos){

21.        this.pos=pos;

22.    }

23.    publicStringgetFileName(){

24.        returnfileName;

25.    }

26.    publicvoidsetFileName(StringfileName){

27.        this.fileName=fileName;

28.    }

29.    publiclonggetFileSize(){

30.        returnfileSize;

31.    }

32.    publicvoidsetFileSize(longfileSize){

33.        this.fileSize=fileSize;

34.    }

35.    @Override

36.    publicStringtoString(){

37.        return"DownloadInfo[url="+url+",pos="+pos+",fileSize="

38.                +fileSize+",fileName="+fileName+"]";

39.    }

40.}

TestDownloadInfo类代码如下:

1.publicclassTestDownloadInfo{

2.    @Test

3.    publicvoidtestSerializeObj(){

4.        try{

5.            DownloadIn

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2