服务器端代码:
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class DownloadServer {
private ServerSocket ss;
File dir = new File("f:/mv");
public DownloadServer(int port)throws Exception{
ss = new ServerSocket(port);
new Thread(){
public void run(){
try {
while(true){
Socket s = ss.accept();
//s交给通信线程
TongxinThread tx = new TongxinThread(s);
tx.start();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("服务器已经停止");
}
}
}.start();
}
class TongxinThread extends Thread{
Socket s;
public TongxinThread(Socket s){
this.s = s;
}
public void run(){
//1.获得目录下文件列表
//2发送列表
//3收取文件号
//4发送文件大小
//5发送文件名
//6发送文件
try {
while(true){
File[] ary = listFiles();
sendFileList(ary);
int n = receiveFileNumber();
sendFileList(ary[n-1].length());
sendFileName(ary[n-1].getName());
sendFile(ary[n-1]);
if(!sendSuccess())