Socket 上传文件

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class FileServer {

	private int port = 1128;
	private ServerSocket servSocket;
	private Socket socket;
	private DataInputStream input;
	private DataOutputStream outPut;
	private String savePath = "E:\\up\\";
	public void startServer() {
		// 已经传输的文件大小
		int ycSize = 0;
		// 文件总大小
		long sumSize = 0;
		// 缓存大小
		int hcSize = 8192;
		// 缓存
		byte[] hc = new byte[hcSize];
		try {
			servSocket = new ServerSocket(port);
			socket = servSocket.accept();
			input = new DataInputStream(new BufferedInputStream(
					socket.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			// 将文件名字读取进来
			savePath += input.readUTF();
			// 文件的长度读取进来(实际只是为了显示进度)
			sumSize = input.readLong();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			outPut = new DataOutputStream(new BufferedOutputStream(
					new FileOutputStream(savePath)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		while (true) {
			int read = 0;
			if (input != null) {
				try {
					read = input.read(hc);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			ycSize += read;
			if (read == -1) {
				break;
			}

			// 下面进度条本为图形界面的prograssBar做的,这里如果是打文件,可能会重复打印出一些相同的百分比
			System.out.println("文件接收了" + (ycSize * 100 / sumSize) + "%\n");
			try {
				outPut.write(hc, 0, read);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		if (outPut != null) {
			try {
				outPut.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			outPut = null;
		}

		if (input != null) {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			input = null;
		}

		if (socket != null) {
			try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			socket = null;
		}

		if (servSocket != null) {
			try {
				servSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			servSocket = null;
		}

		System.out.println("接收完成,文件存为" + savePath + "\n");
	}

	public static void main(String[] args) {
		FileServer fileServer = new FileServer();
		fileServer.startServer();
	}
}

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class FileClient {
	public static void main(String[] args) {

		String filePath = "E:\\test.txt";
		File file = new File(filePath);
		DataInputStream input = null;
		DataOutputStream output = null;
		Socket socket = null;
		try {
			String ip = "5.239";
			int port = 1128;
			socket = new Socket(ip, port);
			input = new DataInputStream(new BufferedInputStream(
					new FileInputStream(filePath)));
			output = new DataOutputStream(socket.getOutputStream());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			output.writeUTF(file.getName());
			output.flush();
			output.writeLong((long) file.length());
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

		int bufferSize = 8192;
		byte[] buf = new byte[bufferSize];

		while (true) {
			int read = 0;
			if (input != null) {
				try {
					read = input.read(buf);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (read == -1) {
				break;
			}

			try {
				output.write(buf, 0, read);
				output.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		try {
			input.close();
			output.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

实现socket文件上传的步骤如下: 1. 客户端读取本地文件,将文件内容通过Socket发送给服务器端。 2. 服务器端接收客户端发送的文件内容,并将文件内容写入到服务器端的硬盘中。 3. 服务器端向客户端发送上传成功的消息。 以下是Python实现socket文件上传的示例代码: 服务器端代码: ```python import os import socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('localhost', 8888)) server_socket.listen(5) print('服务器已启动,等待客户端连接...') while True: client_socket, client_address = server_socket.accept() print('客户端已连接:', client_address) # 读取客户端上传的文件 file_name = client_socket.recv(1024).decode() file_content = client_socket.recv(1024) # 判断d:\upload文件夹是否存在,不存在则创建 if not os.path.exists('d:\\upload'): os.mkdir('d:\\upload') # 将文件内容写入到服务器端的硬盘中 with open('d:\\upload\\' + file_name, 'wb') as f: f.write(file_content) # 给客户端回写“上传成功” client_socket.send('上传成功'.encode()) # 释放资源 f.close() client_socket.close() server_socket.close() ``` 客户端代码: ```python import socket client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect(('localhost', 8888)) # 读取本地文件 file_name = '1.jpg' with open(file_name, 'rb') as f: file_content = f.read() # 将文件内容通过Socket发送给服务器端 client_socket.send(file_name.encode()) client_socket.send(file_content) # 接收服务器端发送的上传成功的消息 print(client_socket.recv(1024).decode()) # 释放资源 f.close() client_socket.close() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值