这里写目录标题

  • Java中通过socket连接传送文件
  • 前言
  • socket嵌套字
  • 发送方
  • 接收方
  • 总结


Java中通过socket连接传送文件

前言

在项目的开发中经常遇到需要传送文件的情况,有时候是通过ftp服务器进行传输,或者是sftp传送。其核心还是通过嵌套字的形式进行数据传输,刨除判别机制和登录机制,写一个简单的基于TCP协议socket网络编程的文件传送。

socket嵌套字

理解:解释客户端与服务端,或者是两个机器之间,一个接收文件另一个发送文件。发送方需要知道要发给谁,这时候就需要知道接收方的ip和端口。
通俗的理解IP就是路,端口就是众多门中的一个约定好的能打开的那个门。

发送方

代码如下,发送的逻辑为:
1.首先通过java中socket对象创建连接
2.实例化一个字节输出数据流
3.获取文件输入串流
4.开始输出文件字节流
5.通知接收方数据传送完成
6.获取接受方响应数据流
7.标记结束项

package action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;


public class SocketCilentAction {
	
	private String host; //目标IP
	private int port; //目标端口
	private String filepath; //文件绝对路径
	
	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
	
	private void sendCilent(String host, int port, String filepath) throws Exception {
		
		//1.首先通过java中socket对象创建连接
		Socket getsocket = new Socket(InetAddress.getByName(host), port);
		//2.实例化一个字节输出数据流
		OutputStream os = getsocket.getOutputStream();
		//3.获取文件输入串流
		FileInputStream files = new FileInputStream(new File(filepath));
		//4.开始输出文件字节流
		byte[] buffer=new byte[1024*10];
		int length;
		/*
		 * socket编程基于I/O流 所以 输出流写转为字节数组的文件 =向接收方发送文件
		 */
		while ((length = files.read(buffer))!=-1){
           os.write(buffer,0,length);
        }
		//5.通知接收方数据传送完成
		getsocket.shutdownOutput();
		//6.获取接受方响应数据流
		InputStream in = getsocket.getInputStream();
		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        byte[] buffer1=new byte[1024*10];
        while ((length=in.read(buffer1))!=-1){
        	bytes.write(buffer1,0,length);
        }
        //7.标记结束项
        System.out.println(bytes.toString());
        
		/* 关闭所有资源 */
        bytes.close();
        files.close();
        os.close();
        getsocket.close();
		

	}

	public static void main(String[] args) throws Exception {
		
		SocketCilentAction socketAction = new SocketCilentAction();
		socketAction.setHost("127.0.0.1");
		socketAction.setPort(8888);
		socketAction.setFilepath("F:\\image\\alias2.jpg");
		socketAction.sendCilent(socketAction.getHost(), socketAction.getPort(),
		socketAction.getFilepath());
	}

}

接收方

接收方的逻辑和发送方基本相似,首先要监听约定好的接收端口,也就是把门给打开,才能够让数据进来。接着就是接收数据,在接收完数据之后返回一个状态就行,可以是一句话、一个字符,也可以是一个文件等等。主要看双方的约定。基本流程:
1.创建监听端口
2.通过socket对象获取输入流获取文件
3.绑定文件路径,用于存放文件
4.返回接收状态

package action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

	public static void main(String[] args) throws IOException{
		
		//1.创建监听端口
		@SuppressWarnings("resource")
		ServerSocket serverSocket = new ServerSocket(8888);
		Socket socket = serverSocket.accept();
		//2.通过socket对象获取输入流获取文件
		InputStream in = socket.getInputStream();
		//3.绑定文件路径,用于存放文件
		FileOutputStream files = new FileOutputStream(new File("F:\\*"));
		int length;
		byte[] buffer1=new byte[1024*10];
        while ((length=in.read(buffer1))!=-1){
        	files.write(buffer1,0,length);
        }
        //4.返回接收状态
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("接收成功".getBytes());
        
		/* 关闭所有资源 */
        files.close();
        in.close();
        //关闭后将不能再监听端口
        //socket.close();
        //serverSocket.close();

	}

}

其中:接收的文件路径中*代表文件储存名称。例如:和所传文件名相同alias2.jpg,也可以是其他。

总结

以上就是最简单的文件串流发送过程,首先要开启接收方程序进行端口的监听,然后通过发送方发送文件。能够绕明白串流的发送过程就行了,主要方法就inout两个部分的使用。