importjava.net.*;importjava.io.*;publicclassServerTcpListenerimplementsRunnable {publicstaticvoidmain(String[] args) {try{finalServerSocket server=newServerSocket(33456);
Thread th=newThread(newRunnable() {publicvoidrun() {while(true) {try{
System.out.println("开始监听...");
Socket socket=server.accept();
System.out.println("有链接");
receiveFile(socket);
}catch(Exception e) {
}
}
}
});
th.run();//启动线程运行}catch(Exception e) {
e.printStackTrace();
}
}publicvoidrun() {
}publicstaticvoidreceiveFile(Socket socket) {byte[] inputByte=null;intlength=0;
DataInputStream dis=null;
FileOutputStream fos=null;try{try{
dis=newDataInputStream(socket.getInputStream());
fos=newFileOutputStream(newFile("d:/cc.jpg"));
inputByte=newbyte[1024];
System.out.println("开始接收数据...");while((length=dis.read(inputByte,0, inputByte.length))>0) {
System.out.println(length);
fos.write(inputByte,0, length);
fos.flush();
}
System.out.println("完成接收");
}finally{if(fos!=null)
fos.close();if(dis!=null)
dis.close();if(socket!=null)
socket.close();
}
}catch(Exception e) {
}
}
}