闲来无事写个socket收发文件的小例子 缺点是发送文件和接收文件路径写死了 这是需求规定的 需要的朋友可以手动改一下 发送文件 package SendFile; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.io.RandomAccessFile; import java.net.Socket; /* * 发送文件 */ public class SendFile extends Thread { String remoteIPString = null;// 远程的字符串 int port; // 远程的服务端口 Socket tempSocket; // 临时套接字 OutputStream outSocket; // 发送文件用的输出流 RandomAccessFile outFile; // 欲发送的文件 String fileName = "E://20100706朝阳.txt"; byte byteBuffer[] = new byte[1024]; // 发送文件用的临时缓存区 static String saveFileIP = "127.0.0.1"; long fileSize = 0; public static void main(String args[]) { SendFile sf = new SendFile(saveFileIP, 12580); sf.run(); } public SendFile(String remoteIPString, int port) { // 构造函数仅用于选择发送文件的位置 // 并从外部接收远程地址和端口 try { this.remoteIPString = remoteIPString; this.port = port; // %%%%%%%%%%%%%%%选择发送的文件位置%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% File file = new File(fileName); fileSize = file.length(); outFile = new RandomAccessFile(file, "r"); file = null; } catch (Exception e) { } } public void run() { try { // 先决条件是服务器端要先开启 this.tempSocket = new Socket(this.remoteIPString, this.port); System.out.println("与服务器连接成功"); outSocket = tempSocket.getOutputStream(); // 发送文件大小 DataOutputStream doc = new DataOutputStream(outSocket); doc.writeUTF(String.valueOf(fileSize)); System.out.println("发送的文件大小为:"+fileSize); // doc.close(); //--------------------------------------- int amount; System.out.println("开始发送文件【"+this.fileName+"】到【"+saveFileIP+"】"); while ((amount = outFile.read(byteBuffer)) != -1) { outSocket.write(byteBuffer, 0, amount); System.out.println("文件发送中..."); } System.out.println("【"+this.fileName+"】已成功发送给【"+saveFileIP+"】"); doc.close(); outFile.close(); tempSocket.close(); }catch (IOException e) { System.out.println("文件发送失败或【"+saveFileIP+"】接收软件未开启请核对~!"); e.printStackTrace(); } } } 接受文件 package SendFile; import java.io.DataInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.ServerSocket; import java.net.Socket; /* * 接收文件 */ public class GetFile extends Thread { private ServerSocket serSocket; // 服务套接字等待 对方的连接和文件发送 private Socket tempSocket; // 由服务套接字产生的 套接字 private InputStream inSocket; // 用于读取 private RandomAccessFile inFile = null; // 随机访问文件 private byte byteBuffer[] = new byte[1024]; // 临时缓寸区 private int defaultBindPort = 12580; // 默认用10000端口监听请求 private int tryBindTimes = 0; // 初始的绑定端口次数为0 private int currentBindPort = defaultBindPort + tryBindTimes;// 当前绑定的端口号是10000默认端口 private String filePath = ""; //接收到文件的保存路径 private String fileSize = ""; private DataInputStream input; //绑定端口 private void bindToServerPort() throws Exception{// 抛出异常的原因是无法绑定服务的端口 try { System.out.println("试绑定的端口号是:" + this.currentBindPort);// 输出绑定的端口号到当前的控制台上 serSocket = new ServerSocket(this.currentBindPort); // 在自己的机器上开一个服务类套接字 // 并等待发送者的连接 } catch (Exception e) { e.printStackTrace(); System.out.println(e.toString());// 绑定不成功重试 this.tryBindTimes = this.tryBindTimes + 1;// 试了不止一次了 // 可查看试的次数getTryBindedTimes this.currentBindPort = this.defaultBindPort + this.tryBindTimes;// if (this.tryBindTimes >= 20) { throw new Exception("无法绑定到指定端口" + '/n' + "试了太多次了!");// 如果试的次数超过20次 退出 } this.bindToServerPort();// 递归的绑定 } System.out.println("成功绑定的端口号是:" + this.currentBindPort);// 输出绑定的端口号到当前的控制台上 } public int getTryBindedTimes() { return this.tryBindTimes; } public int getCurrentBindingPort() { return this.currentBindPort; } public GetFile(int port) throws Exception{// 抛出异常的原因是无法绑定服务的端口 try { this.bindToServerPort();// 绑定服务的端口 } catch (Exception e) { e.printStackTrace(); System.out.println(e.toString());// 绑定不成功重试 throw new Exception("绑定端口不成功!"); } filePath = "c://File"; //添加路径 如果路径不存在创建路径 File savedFile = new File(filePath); if(!savedFile.exists()){ savedFile.mkdir(); } filePath = filePath + "//Save.txt"; // 固定文件格式 savedFile = new File(filePath ); if (savedFile != null){ inFile = new RandomAccessFile(savedFile, "rw");// 用以读取数据的随机访问文件 // 可以每次以块的方式读取数据 } savedFile = null; } public void run() { try { /* if (this.inFile == null) { System.out.println("没有选择文件"); this.serSocket.close();// 关闭服务方套接字 return; // 没有选择文件 }*/ System.out.println("wait for..." + '/n' + "等待对方接入"); tempSocket = serSocket.accept(); // 等待对方的连接 this.serSocket.setSoTimeout(5000);// 五秒钟连不上将抛出异常 this.inSocket = tempSocket.getInputStream(); // 获取输入流 //接收文件大小 try { input = new DataInputStream(tempSocket.getInputStream()); fileSize = input.readUTF(); System.out.println("接受的文件大小为: " + fileSize); } catch (IOException e) { e.printStackTrace(); } } catch (Exception ex) { System.out.println(ex.toString()); ex.printStackTrace(); return; } int amount; // 以下为传送文件代码 和流 套接字清理工作 try { while ((amount = inSocket.read(byteBuffer)) != -1) { inFile.write(byteBuffer, 0, amount); } //验证文件完整度 File f = new File(filePath); if(f.length() != Long.parseLong(fileSize)){ System.out.println("接收到的文件大小与服务器不同,请重新发送~!"); }else{ System.out.println("接收到的文件大小与服务器相同,验证完毕~!"); } //--------------------------------------- input.close(); inSocket.close(); // 关闭流 inFile.close(); // 关闭文件 tempSocket.close(); // 关闭临时套接字 this.serSocket.close();// 关闭服务方套接字 } catch (IOException e) { System.out.println(e.toString()); e.printStackTrace(); } } public static void main(String args[]) { GetFile gf = null; try { gf = new GetFile(10000); } catch (Exception e) { e.printStackTrace(); System.out.println("无法传送文件!"); System.exit(1); } gf.run(); System.out.println("end"); } }