import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
System.out.println("客服端发送文件");
System.out.println("输入服务端的端口号:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
int port = Integer.parseInt(br.readLine().trim());
System.out.println("输入发送的文件");
String filename = br.readLine().trim();
new Client().sendFile(filename, port);
System.out.println("发送完毕!");
} catch (Exception e) {
}
}
public void sendFile(String filename, int port) {
File file = new File(filename);
if (!file.exists())
return;
Socket socket = null;
DataOutputStream fout = null;
try {
DataInputStream fin = new DataInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int loop = fin.available() / 1024 + 1;
int flag = loop - 1;
for (int i = 0; i < loop; i++) {
fin.read(buffer);
socket = new Socket("localhost", port);
fout = new DataOutputStream(socket.getOutputStream());
Thread.sleep(50);
fout.write(buffer);
fout.close();
socket.close();
if (i != flag)
buffer = new byte[1024];
}
socket = new Socket("localhost", port);
fout = new DataOutputStream(socket.getOutputStream());
fout.write("end".getBytes());
fout.close();
socket.close();
fin.close();
} catch (Exception e) {
}
}
}
- 1
- 2
- 3
- 4
- 5
前往页