点对点文件传输,java的,恩人来看看吧
发布网友
发布时间:2022-05-03 08:14
我来回答
共3个回答
热心网友
时间:2023-10-16 05:30
接收器
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 port
808 -- 这行是输入的
Receiver started --这行完事后会等发送文件File transfer comes,Please input a path to put the file
d:\a.rar --这行是输入的,输入文件路径,包含文件名
start transfer
Complete
-----------------------------------------
发送器用法:
please tell me the ip address of the target computer
127.0.0.1 --输入的,目的地址
please tell me the linstening port of the target computer
808 --目的端口,和接收器输入的那个要匹配
please input the source path of your file
G:\WIN7 Activation.rar --输入文件全路径包含文件名
start transfer
Complete
先开recevier,再用sender发送。
热心网友
时间:2023-10-16 05:30
那就用飞鸽好了0.0 java有引用语句 调用他就ok了!
热心网友
时间:2023-10-16 05:31
不是B/S的 那你要C/S的?
热心网友
时间:2023-10-16 05:30
接收器
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 port
808 -- 这行是输入的
Receiver started --这行完事后会等发送文件File transfer comes,Please input a path to put the file
d:\a.rar --这行是输入的,输入文件路径,包含文件名
start transfer
Complete
-----------------------------------------
发送器用法:
please tell me the ip address of the target computer
127.0.0.1 --输入的,目的地址
please tell me the linstening port of the target computer
808 --目的端口,和接收器输入的那个要匹配
please input the source path of your file
G:\WIN7 Activation.rar --输入文件全路径包含文件名
start transfer
Complete
先开recevier,再用sender发送。
热心网友
时间:2023-10-16 05:30
那就用飞鸽好了0.0 java有引用语句 调用他就ok了!
热心网友
时间:2023-10-16 05:31
不是B/S的 那你要C/S的?