6.服务器与客户端双线程传输文件--客户端

来源:互联网 发布:怀孕食谱软件 编辑:程序博客网 时间:2024/05/22 05:07
/* * 本程序主要功能: * 客户端,双线程传输文件 * 其中一个线程向服务器端发送文件,一个线程从服务器端下载文件 *  */import java.io.*;//这里使用BufferedInputStream,BufferedReader,DataInputStream,File,//IOException,InputStream,InputStreamReader,PrintWriter,//RandomAccessFile 类import java.net.*;//这里使用Socket,UnknownHostException 类public class TestFileClient_1 {public static String IP = "localhost";//Server的IP地址:localhostpublic static int DEFAULT_PORT = 8000;//Server的端口号:8000public Socket client = null;//新建一个Socket成员属性:clientpublic GetFile gf;//新建一个GetFile成员属性:gfpublic SendFile sf;//新建一个SendFile成员属性:sfpublic void start()//方法:启动服务{try{System.out.println("try to connect to Server...");//提示信息:尝试连接服务器client = new Socket(IP,DEFAULT_PORT);//通过IP和端口号连接服务器,返回Socket对象,赋值给clientif(client != null)//client不为null时,成功连接服务器{System.out.println("Connect successfully!");//提示信息:成功连接服务器sf = new SendFile(client);//通过client,创建一个SendFile对象:sfgf = new GetFile(client);//通过client,创建一个GetFile对象:gfsf.start();//启动sf对应的SendFile线程gf.start();//启动gf对应的GetFile线程}}catch(Exception e){e.printStackTrace();}}public static void main(String[] args) {// TODO 自动生成的方法存根TestFileClient_1 tfc = new TestFileClient_1();//创建一个TestFileClient_1对象:tfctfc.start();//tfc启动服务}}class SendFile extends Thread{public Socket client;//新建一个Socket成员属性:clientpublic FileInputStream fileInput;//新建一个InputStream成员属性:fileInputpublic DataOutputStream fileOutput;//新建一个OutputStream成员属性:fileOutputbyte[] buf;//新建一个字节数组成员属性:bufpublic SendFile(Socket client){this.client = client;//将形参client赋值给属性clienttry{buf = new byte[1024];//创建byte数组大小为1024个字节fileOutput = new DataOutputStream(//创建一个数据输出流new BufferedOutputStream(//创建一个缓冲输出流this.client.getOutputStream()));//获取client的输出流fileInput = new FileInputStream(new File("isClient.pdf"));//创建一个文件输入流,获取文件isClient.pdf的内容}catch(Exception e){e.printStackTrace();}}@Override//重写标记public void run()//实现run方法{try{/*开始发送文件*/int num = fileInput.read(buf);//从此输入流中将最多 buf.length个字节的数据读入一个 buf数组中。返回:读入缓冲区的字节总数while(num != -1)//如果因为已经到达文件末尾而没有更多的数据,则返回 -1。 {fileOutput.write(buf, 0, num);//将buf数组中从0 开始的num个字节写入此文件输出流。num = fileInput.read(buf);//从此输入流中将最多 buf.length个字节的数据读入一个 buf数组中。返回:读入缓冲区的字节总数fileOutput.flush();//刷新输入缓冲区,并强制将输入缓冲区所有字节写出至目的地址}/*关闭流处理*/fileInput.close();//关闭fileInput流client.shutdownOutput();//禁用client的输出流。//注意:这里不能使用OutputStream.close()方法。//因为OutputStream.close()方法会将Socket关闭,使得GetFile线程造成异常//也不能不进行关闭,因为不关闭会造成阻塞,无法正常使用GetFile的run()方法}catch(Exception e){e.printStackTrace();try{if(fileInput != null){fileInput.close();//异常时,如果已经打开了fileInput,则关闭}if(fileOutput != null){fileOutput.close();//异常时,如果已经打开了fileOutput,则关闭}}catch(Exception e1){e1.printStackTrace();}}}}class GetFile extends Thread{public Socket client;//新建一个Socket成员属性:clientpublic DataInputStream fileInput;//新建一个DataInputStream成员属性:fileInputpublic File file;//新建一个File成员属性:filepublic RandomAccessFile rf;//新建一个RandomAccessFile成员属性:rfpublic byte[] buf;public GetFile(Socket client){this.client = client;//将形参client赋值给属性clienttry{buf = new byte[1024];//创建byte数组buf大小为1024个字节file = new File("fromServer.pdf");//通过文件名"fromServer.pdf",创建一个File对象:filefile.createNewFile();//当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。rf = new RandomAccessFile(file,"rw");//通过file,创建从中读取和向其中写入的随机访问文件流对象:rf。//"rw" 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。 fileInput = new DataInputStream(//创建一个数据输入流new BufferedInputStream(//创建一个缓冲输入流this.client.getInputStream()));//获取client的输入流}catch(Exception e){e.printStackTrace();}}@Override//重写标记public void run()//实现run方法{try{System.out.println("Downloading File...");//提示信息:下载文件中.../*开始下载文件*/int num = fileInput.read(buf);//从此输入流中将最多 buf.length个字节的数据读入一个 buf数组中。返回:读入缓冲区的字节总数while(num != -1)//如果因为已经到达文件末尾而没有更多的数据,则返回 -1{rf.write(buf, 0, num);//将buf数组中从0 开始的num个字节写入此文件输出流rf.skipBytes(num);//跳过num个字节数num = fileInput.read(buf);//从此输入流中将最多 buf.length个字节的数据读入一个 buf数组中。返回:读入缓冲区的字节总数}/*关闭流处理*/rf.close();//关闭RandomAccessFile流client.shutdownInput();//禁用client的输入流。//注意:这里不能使用InputStream.close()方法。//因为InputStream.close()方法会将Socket关闭,使得SendFile线程造成异常//也不能不进行关闭,因为不关闭会造成阻塞,无法正常使用SendFile的run()方法System.out.println("Downloading Completely");//提示信息:下载完成}catch(Exception e){e.printStackTrace();try{if(fileInput != null){fileInput.close();//异常时,如果已经打开了fileInput,则关闭}}catch(Exception e1){e1.printStackTrace();}}}}

0 0
原创粉丝点击