packagetest;importjava.io.DataInputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;public class ServerTcpListener implementsRunnable {
@Overridepublic voidrun() {
}public static voidmain(String[] args) {try{final ServerSocket server = new ServerSocket(33456);
Thread th= new Thread(newRunnable() {
@Overridepublic voidrun() {while (true) {try{
System.out.println("开始监听。。。");
Socket socket=server.accept();
System.out.println("有链接");
receiveFile(socket);
}catch(Exception e) {
e.printStackTrace();
}
}
}
});
th.run();
}catch(Exception ex) {
ex.printStackTrace();
}
}public static void receiveFile(Socket socket) throwsIOException {byte[] inputByte = null;int length = 0;
DataInputStream din= null;
FileOutputStream fout= null;try{
din= newDataInputStream(socket.getInputStream());
fout= new FileOutputStream(new File("E:\\"+din.readUTF()));
inputByte= new byte[1024];
System.out.println("开始接收数据...");while (true) {if (din != null) {
length= din.read(inputByte, 0, inputByte.length);
}if (length == -1) {break;
}
System.out.println(length);
fout.write(inputByte,0, length);
fout.flush();
}
System.out.println("完成接收");
}catch(Exception ex) {
ex.printStackTrace();
}finally{if (fout != null)
fout.close();if (din != null)
din.close();if (socket != null)
socket.close();
}
}
}