📄 myclient.java
字号:
package client;
import java.net.*;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class MyClient extends Thread {
String iPString = null; // 服务器IP
int port; // 远程的服务端口
Socket socket; // 客户端socket
OutputStream outSocket; // 发送文件用的输出流
RandomAccessFile outFile; // 欲发送的文件
byte byteBuffer[] = new byte[1024]; // 发送文件用的临时缓存区
public static void main(String args[]) throws IOException {
MyClient client = new MyClient("127.0.0.1", 4988);
client.start();
}
//--------构造函数仅用于选择发送文件的位置并从外部接收远程地址和端口-------------
public MyClient(String iPString, int port) {
try {
this.iPString = iPString;
this.port = port;
JFileChooser jfc = new JFileChooser("./src");
File file = null;
int returnVal = jfc.showOpenDialog(new javax.swing.JFrame());
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = jfc.getSelectedFile();
}
outFile = new RandomAccessFile(file, "r");
} catch (Exception e) {
}
}
//------------------------运行客户端-------------------------------------------------
public void run() {
try {
int amount;
this.socket = new Socket(this.iPString, this.port);
System.out.println("Connect server successfully!");
outSocket = socket.getOutputStream();
System.out.println("Start sending file...");
while ((amount = outFile.read(byteBuffer)) != -1) {
outSocket.write(byteBuffer, 0, amount);
}
JOptionPane.showMessageDialog(new javax.swing.JFrame(),
"send successfully!", "提示!", JOptionPane.PLAIN_MESSAGE);
System.out.println("send successfully!");
outFile.close();
socket.close();
} catch (IOException e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -