1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > java实现TCP协议文件传输

java实现TCP协议文件传输

时间:2019-06-30 18:36:37

相关推荐

java实现TCP协议文件传输

/*** 需求:将指定文件从D盘目录d:\1下移动到d:\2下* @param args* @throws IOException*/public static void main(String[] args) throws IOException {//1. 建立输入流和输出流FileInputStream fis = new FileInputStream("d:\\1\\1.png");//源为d盘1目录下文件1.pngFileOutputStream fos = new FileOutputStream("d:\\2\\2.png");//目的为d盘2目录下文件2.png//2. 使用自定义缓冲区将输入流和输出流关联起来byte[] buf = new byte[1024];// 定义1024byte的缓冲区int len = 0;// 输入流读到缓冲区中的长度//3. 将数据从输入流循环读入缓冲区, 当读到文件最后,会得到值-1while((len=fis.read(buf))!=-1){fos.write(buf,0,len);//将读到长度部分写入输出流}//4. 关闭流fis.close();fos.close();}

客户端发数据到服务端

/** TCP传输,客户端建立的过程* 1,创建TCP客户端Socket服务,使用的是Socket对象。* 建议该对象一创建就明确目的地。要连接的主机。* 2,如果连接建立成功,说明数据传输通道已建立。* 该通道就是Socket流,是底层建立好的。既然是流,说明这里既有输入,又有输出。* 3,使用输出流,将数据写出。* 4,关闭资源。*/// 建立客户端SocketSocket s = new Socket(InetAddress.getLocalHost(), 9003);// 获得输出流OutputStream out = s.getOutputStream();// 获得输入流FileInputStream fis = new FileInputStream("d:\\1\\1.png");// 发送文件信息byte[] buf = new byte[1024];int len = 0;while ((len = fis.read(buf)) != -1) {// 写入到Socket输出流out.write(buf, 0, len);}s.shutdownOutput();// 关流out.close();fis.close();s.close();

服务端

ServerSocket ss = new ServerSocket(9003);// 需要指定端口,客户端与服务端相同,一般在1000-65535之间//服务端一般一直开启来接收客户端的信息。while (true) {// 获取客户端SocketSocket s = ss.accept();// 获取输入流与输出流InputStream in = s.getInputStream();// 输入流FileOutputStream fos = new FileOutputStream("d:\\3\\3.png");// 创建缓冲区关联输入流与输出流byte[] buf = new byte[1024];int len = 0;// 数据的写入while ((len = in.read(buf)) != -1) {fos.write(buf, 0, len);}// 关流fos.close();s.close();}

完整代码:

import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .InetAddress;import .Socket;import .UnknownHostException;public class Client {public static void main(String[] args) throws UnknownHostException, IOException {/** 客户端先向服务端发送一个文件名,服务端接收到后给客户端一个反馈,然后客户端开始发送文件*///建立客户端SocketSocket s = new Socket(InetAddress.getLocalHost(), 9001);//修改为服务器IP地址//获得输出流OutputStream out = s.getOutputStream();//关联发送文件File file = new File("D:\\1.png");String name = file.getName();//获取文件完整名称String[] fileName = name.split("\\.");//将文件名按照.来分割,因为.是正则表达式中的特殊字符,因此需要转义String fileLast = fileName[fileName.length-1];//后缀名//写入信息到输出流out.write(name.getBytes());//读取服务端的反馈信息InputStream in = s.getInputStream();byte[] names = new byte[50];int len = in.read(names);String nameIn = new String(names, 0, len);if(!fileLast.equals(nameIn)){//结束输出,并结束当前线程s.close();System.exit(1);}//如果正确,则发送文件信息//读取文件信息FileInputStream fr = new FileInputStream(file);//发送文件信息byte[] buf = new byte[1024];while((len=fr.read(buf))!=-1){//写入到Socket输出流out.write(buf,0,len);}//关流out.close();fr.close();s.close();}}

服务端 任务类

import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import .Socket;public class Task implements Runnable {private Socket s;public Task(Socket s){this.s = s;}@Overridepublic void run() {String ip = s.getInetAddress().getHostAddress();try{//获取客户端输入流InputStream in = s.getInputStream();//读取信息byte[] names = new byte[100];int len = in.read(names);String fileName = new String(names, 0, len);String[] fileNames = fileName.split("\\.");String fileLast = fileNames[fileNames.length-1];//然后将后缀名发给客户端OutputStream out = s.getOutputStream();out.write(fileLast.getBytes());//新建文件File dir = new File("d:\\server\\"+ip);if(!dir.exists())dir.mkdirs();File file = new File(dir,fileNames[0]+"."+fileLast);FileOutputStream fos = new FileOutputStream(file);//将Socket输入流中的信息读入到文件byte[] bufIn = new byte[1024];while((len = in.read(bufIn))!=-1){//写入文件fos.write(bufIn, 0, len);}fos.close();s.close();}catch(Exception e){e.printStackTrace();}}}

服务器类

import java.io.IOException;import .ServerSocket;import .Socket;public class Server {public static void main(String[] args) throws IOException {/** 服务端先接收客户端传过来的信息,然后向客户端发送接收成功,新建文件,接收客户端信息*///建立服务端ServerSocket ss = new ServerSocket(9001);//客户端端口需要与服务端一致while(true){//获取客户端SocketSocket s = ss.accept();new Thread(new Task(s)).start();}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。