一、表示Socket服务端的类:Server.java
Socket客户端处理类:SocketConnection.java
二、表示客户端的类:Client.java
public class Server extends Thread{
public static int port = 6789;
public static String host = "10.60.1.127";
private static ServerSocket server = null;
public void run() {
if(server == null){
try{
//1、新建ServerSocket实例
server = new ServerSocket(port);
}catch(IOException e){
e.printStackTrace();
}
}
System.out.println("服务器启动...");
while(true){
try{
//2、访问ServerSocket实例的accept方法取得一个客户端Socket对象
Socket client = server.accept();
if(client == null) continue;
new SocketConnection(client, "D:\\").start();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new Server().start();
}
}
Socket客户端处理类:SocketConnection.java
public class SocketConnection extends Thread{
private Socket client;
private String filepath;
public SocketConnection(Socket client, String filepath){
this.client = client;
this.filepath = filepath;
}
public void run(){
if(client == null) return;
DataInputStream in = null;
DataOutputStream writer = null;
try{
//3、访问Socket对象的getInputStream方法取得客户端发送过来的数据流
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
String fileName = in.readUTF(); //取得附带的文件名
if(filepath.endsWith("/") == false && filepath.endsWith("\\") == false){
filepath += "\\";
}
filepath += fileName;
//4、将数据流写到文件中
writer = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filepath)))));
int bufferSize = 2048;
byte[] buf = new byte[bufferSize];
int read = 0;
while((read=in.read(buf)) != -1){
writer.write(buf, 0, read);
}
writer.flush();
System.out.println("数据接收完毕");
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
in.close();
writer.close();
client.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
二、表示客户端的类:Client.java
public class Client {
private Socket client;
private boolean connected;
public boolean isConnected() {
return connected;
}
public void setConnected(boolean connected) {
this.connected = connected;
}
public Client(String host, int port){
try {
//1、新建Socket对象
client = new Socket(host, port);
System.out.println("服务器连接成功!");
this.connected = true;
} catch (UnknownHostException e) {
this.connected = false;
close();
} catch (IOException e) {
System.out.println("服务器连接失败!");
this.connected = false;
close();
}
}
/**
* 将文件内容发送出去
*
* @param filepath 文件的全路径名
*/
public void sendFile(String filepath){
DataOutputStream out = null;
DataInputStream reader = null;
try{
if(client == null) return;
File file = new File(filepath);
reader = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
//2、将文件内容写到Socket的输出流中
out = new DataOutputStream(client.getOutputStream());
out.writeUTF(file.getName()); //附带文件名
int bufferSize = 2048; //2K
byte[] buf = new byte[bufferSize];
int read = 0;
while((read=reader.read(buf)) != -1){
out.write(buf, 0, read);
}
out.flush();
}catch(IOException ex){
ex.printStackTrace();
close();
}finally{
try {
reader.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭Socket
*/
public void close(){
if(client != null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client(Server.host, Server.port);
if(client.isConnected()){
client.sendFile("E:\\EKI.txt");
client.close();
}
}
}