1.传送文本文件

1.1服务端

package com;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket; public class FileServerSocket {
public static void main(String[] args) {
try {
File file = new File("F:\\Test\\129.mp3"); ServerSocket server = new ServerSocket(8008);
DataInputStream dataInput;
String fileMessage = ""; FileWriter fileWriter = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fileWriter); Socket socket;
System.out.println("服务器已启动~~~");
socket = server.accept();
System.out.println("捕获到连接");
while(true){
dataInput = new DataInputStream(socket.getInputStream());
fileMessage = dataInput.readUTF(); bw.write(fileMessage);
bw.flush();
System.out.println(fileMessage);
if(fileMessage.equals("exit")){
System.out.println("传输完成~~~");
break;
}
} bw.close();
dataInput.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

1.2 ### 客户端

package com;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException; public class FileSocket {
public static void main(String[] args) {
try {
File file = new File("F:\\Test\\12.mp3");
Socket socket = new Socket("127.0.0.1",8008);
OutputStream output = socket.getOutputStream(); FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader); DataOutputStream dataOutput = new DataOutputStream(output);
dataOutput.writeUTF(file.getName()); String sb = "";
System.out.println("客户端已启动");
while((sb = br.readLine()) != null){
dataOutput.writeUTF(sb);
dataOutput.flush();
}
System.out.println("客户端文件已经传送完成");
dataOutput.close();
br.close();
socket.close(); } catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

2.传送其他文件

2.1 服务端

package fileSocket;

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket; public class Server {
public static void main(String[] args) throws EOFException {
try {
ServerSocket server = new ServerSocket(8008);
DataInputStream dataInput; File file = new File("F:\\Test\\12_3.mp3");
FileOutputStream fileOutput = new FileOutputStream(file); Socket socket;
System.out.println("服务器已启动~~~"); byte byte_[] = new byte[1024 * 4];
int length; socket = server.accept();
System.out.println("捕获到连接");
dataInput = new DataInputStream(socket.getInputStream());
while((length = dataInput.read(byte_,0,byte_.length)) > 0){
fileOutput.write(byte_,0,length);
fileOutput.flush();
}
fileOutput.close();
dataInput.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("接收完成"); }
}

2.2 客户端

package fileSocket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException; public class Client {
public static void main(String[] args) {
try {
File file = new File("F:\\Test\\12.mp3");
FileInputStream fileStream = new FileInputStream(file); Socket socket = new Socket("127.0.0.1",8008);
OutputStream output = socket.getOutputStream();
DataOutputStream dataOutput = new DataOutputStream(output); byte[] b = new byte[1024 * 4];
System.out.println("客户端已启动");
int length;
while((length = fileStream.read(b,0,b.length)) > 0){
dataOutput.write(b,0,length);
dataOutput.flush();
}
System.out.println("客户端文件已经传送完成");
dataOutput.close();
fileStream.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
05-11 15:15