服務(wù)器端類(lèi) SocketServer
public class SocketServer {
protected int listenPort = 3000;
public static void main(String[] args) {
SocketServer server = new SocketServer();
server.acceptConnections();
}
/**
* 建立鏈接
*
*/
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(listenPort);
Socket socket = null;
while (true) {
socket = server.accept();
//handleConnection(socket);
new ServerThread(socket).start();
}
} catch (BindException e) {
System.out.println("Unable to bind to port " + listenPort);
} catch (IOException e) {
System.out.println("Unable to instantiate a ServerSocket on port: "
+ listenPort);
}
}
}
服務(wù)器端接收線(xiàn)程類(lèi)
public class ServerThread extends Thread {
private Socket socket;
public ServerThread(Socket socket)
{
this.socket = socket;
}
public void run(){
try {
DataInputStream inputStream = null;
try {
inputStream = new DataInputStream(new BufferedInputStream(
socket.getInputStream()));
} catch (Exception e) {
System.out.print("接收消息緩存錯誤\n");
return;
}
try {
// 本地保存路徑,文件名會(huì )自動(dòng)從服務(wù)器端繼承而來(lái)最好是web工程里的一個(gè)路徑。
String savePath = "E:\\";
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
long len = 0;
savePath += inputStream.readUTF();
DataOutputStream fileOut = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(savePath))));
len = inputStream.readLong();
System.out.println("文件的長(cháng)度為:" + len + "\n");
System.out.println("開(kāi)始接收文件!" + "\n");
while (true) {
int read = 0;
if (inputStream != null) {
read = inputStream.read(buf);
}
if (read == -1) {
break;
}
//System.out.println(buf.toString());
fileOut.write(buf, 0, read);
}
System.out.println("接收完成,文件存為" + savePath + "\n");
fileOut.flush();
fileOut.close();
inputStream.close();
} catch (Exception e) {
System.out.println("接收消息錯誤" + "\n");
return;
}
} catch (Exception e) {
System.out.println("Error handling a client: " + e);
}
}
}
客戶(hù)端發(fā)送文件類(lèi)
public class SocketClient extends Thread{
protected String hostIp ="127.0.0.1";
protected int hostPort = 3000;
InputStream fis;
DataOutputStream ps;
File fi;
DataInputStream dis;
String path;
Socket client;
public SocketClient() {
}
public SocketClient(String path) {
this.path = path;
}
public SocketClient(String path,String aHostIp, int aHostPort) {
this.path = path;
this.hostIp = aHostIp;
this.hostPort = aHostPort;
}
public static void main(String[] args) {
SocketClient client = new SocketClient();
String path = "f:\\35.xml"; //測試文件
client.sendMessage();
}
public void run(){
sendMessage();
}
/**
* 將連接到遠程服務(wù)器
*/
public void setUpConnection() {
try {
client = new Socket(hostIp, hostPort);
fis = new FileInputStream(path);
ps = new DataOutputStream(client.getOutputStream());
fi = new File(path);
} catch (UnknownHostException e) {
System.out
.println("Error setting up socket connection: unknown host at "
+ hostIp + ":" + hostPort);
} catch (IOException e) {
System.out.println("Error setting up socket connection: " + e);
}
}
/**
* 將向遠程服務(wù)器請求 path 的內容
*
* @param path
* @return
*/
public void sendMessage() {
setUpConnection();
if(client == null)
return ;
try {
// 將文件名及長(cháng)度傳給客戶(hù)端。
ps.writeUTF(fi.getName());
ps.flush();
ps.writeLong((long) fi.length());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read == -1) {
break;
}
ps.write(buf, 0, read);
}
ps.flush();
System.out.println("文件傳輸完成");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 注意關(guān)閉socket鏈接哦,不然客戶(hù)端會(huì )等待server的數據過(guò)來(lái),
// 直到socket超時(shí),導致數據不完整。
try {
ps.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
聯(lián)系客服