点对点文件传输,java的,恩人来看看吧

[复制链接]
查看11 | 回复1 | 2010-4-27 11:27:18 | 显示全部楼层 |阅读模式
接收器import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;public class Reveiver { public static void main(String[] args) throws Exception{Scanner scanner = new Scanner(System.in);System.out.println("please tell me the linstening port");int port = Integer.parseInt(scanner.nextLine());System.out.println("Receiver started");
ServerSocket ssocket = new ServerSocket(port);while(true) { Socket s = ssocket.accept(); System.out.println("File transfer comes,Please input a path to put the file");
String line = null; while(line==null||line.equals("")) {
line = scanner.nextLine(); } System.out.println("start transfer"); OutputStream os = new FileOutputStream(line); InputStream is = s.getInputStream(); byte[] buffer= new byte[1024]; int cnt = 0; while((cnt=is.read(buffer))>=0) {
os.write(buffer,0,cnt); } os.close(); is.close(); System.out.println("Complete");
} }}发送器:import java.io.File;import java.io.FileInputStream;import java.io.OutputStream;import java.net.Socket;import java.util.Scanner;public class Sender { public static void main(String[] args) throws Exception {Scanner scanner = new Scanner(System.in);System.out.println("please tell me the ip address of the target computer");String host = scanner.nextLine();System.out.println("please tell me the linstening port of the target computer");int port = Integer.parseInt(scanner.nextLine());Socket socket = new Socket(host, port);System.out.println("please input the source path of your file");File f = new File(scanner.nextLine());while (!f.exists()) { System.out.println("file you inputed does not exist"); f = new File(scanner.nextLine());}System.out.println("start transfer");FileInputStream fis = new FileInputStream(f);byte[] buffer= new byte[1024];int cnt = 0;OutputStream os = socket.getOutputStream();while((cnt=fis.read(buffer))>=0) { os.write(buffer,0,cnt);}os.close();fis.close();System.out.println("Complete"); }}------------------------------------------接收器用法please tell me the linstening port808-- 这行是输入的Receiver started --这行完事后会等发送文件File transfer comes,Please input a path to put the filed:\a.rar--这行是输入的,输入文件路径,包含文件名start transferComplete-----------------------------------------发送器用法:please tell me the ip address of the target computer127.0.0.1 --输入的,目的地址please tell me the linstening port of the target computer808 --目的端口,和接收器输入的那个要匹配please input the source path of your fileG:\WIN7 Activation.rar --输入文件全路径包含文件名start transferComplete先开recevier,再用sender发送。
回复

使用道具 举报

千问 | 2010-4-27 11:27:18 | 显示全部楼层
不是B/S的 那你要C/S的?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行