packagecn.com.test09;importjava.io.DataInputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;public classt10 {public static voidmain(String[] args) {new ServerO("F:\\",8080);
}
}classServerO {privateString path;private intport;privateServerSocket ss;public ServerO(String s, intp) {this.path =s;this.port =p;try{
ss= newServerSocket(port);while (true) {
Socket soc=ss.accept();new Thread(newOOOO(soc,path)).start();
}
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();try{
ss.close();
}catch(IOException e1) {//TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}class OOOO implementsRunnable {privateSocket soc;privateString path;publicOOOO(Socket soc,String pa) {this.soc =soc;this.path=pa;
}public void setFile(String name,byte[] b,inti){try{
FileOutputStream bos= new FileOutputStream(new File(path+name),true);
bos.write(b,0,i);
bos.close();
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
System.out.println("保存文件出错");
}
}
@Overridepublic voidrun() {//TODO Auto-generated method stub
DataInputStream in = null;
String name;byte[] b= new byte[1024];inti;try{
in= newDataInputStream(soc.getInputStream());
name=in.readUTF();while((i=in.read(b))!=-1){
setFile(name, b ,i);
}
}catch(IOException e) {//TODO Auto-generated catch block
System.out.println("run中出现异常");
e.printStackTrace();
}finally{try{
in.close();
soc.close();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}