Socket传送文件

本文介绍了一个基于Java Socket的简单文件传输应用,包括客户端和服务端的实现细节。客户端能够发送指定路径下的文件到服务端,服务端接收并保存文件。通过缓存流提高读取效率,使用DataOutputStream发送文件大小及内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端:

Client.java

package lee.socket;

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;

public class Client {
	static DataInputStream din = null;
	static DataOutputStream dout = null;
	static Socket s = null;

	public static void main(String[] args) {
		try {
			s = new Socket("127.0.0.1", 2224);

			File file = new File("d:/05.jpg"); // 定义文件
			FileInputStream fis = new FileInputStream(file); // 定义文件输入流
			din = new DataInputStream(new BufferedInputStream(fis)); // 用缓存流包装文件输入流(提高读取速度),然后再包装成数据输入流
			dout = new DataOutputStream(s.getOutputStream());// 定义数据输出流

			dout.writeUTF(String.valueOf(file.length())); // 发送文件长度

			byte[] buffer = new byte[1024]; // 定义缓存
			int len = 0;
			while ((len = din.read(buffer)) != -1) {
				dout.write(buffer, 0, len); // 向服务器发送数据
			}
			dout.flush();

		} catch (IOException e) {
			System.out.println(e.toString());
		} finally {
			try {
				if (din != null) {
					din.close();
					din = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if (dout != null) { // 最后一定要关闭输出流,不然数据发送不出去。导致一直连接着,不断开
					dout.close();
					dout = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if (s != null) {
					s.close();
					s = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

  

 Android和客户端基本一样。

服务端:

Server.java

package lee.socket;

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

public class Server {
	static DataInputStream din = null;
	static DataOutputStream dout = null;
	static Socket s = null;

	public static void main(String[] args) {
		new Thread() { // 开启子线程
			public void run() {
				try {
					ServerSocket ss = new ServerSocket(2224); // 这个必须在while外,不然会循环连接端口,出错
					while (true) {
						System.out.println("--------等待用户连接--------------");
						s = ss.accept();
						System.out.println("--------用户连接上了--------------");

						din = new DataInputStream(new BufferedInputStream(s
								.getInputStream()));// 使用缓存进行包装,提示读取速度
						System.out.println("文件长度:" + din.readUTF()); // 显示接收文件长度

						File file = new File("d:/01.jpg");
						FileOutputStream fos = new FileOutputStream(file);
						dout = new DataOutputStream(new BufferedOutputStream(
								fos));

						byte[] buffer = new byte[1024];
						int len = 0;
						while ((len = din.read(buffer)) != -1) {
							dout.write(buffer, 0, len);
						}
						dout.flush();
						dout.close(); // 下面的finally要等到循环结束后才执行,如果不执行close,文件无法正常打开

						System.out.println("接受成功");
					}

				} catch (IOException e) {
					e.printStackTrace();
				} finally { // 等while循环结束后才会执行,
					try {
						if (dout != null) {
							dout.close();
							dout = null;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					try {
						if (din != null) {
							din.close();
							din = null;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					try {
						if (s != null) {
							s.close();
							s = null;
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}.start();

	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值