原文
import java.io.*;
import java.net.*;
public class MyClient
{
Socket client;
boolean flag=true;
FileInputStream fis;
DataOutputStream dos;
DataInputStream dis;
public static void main(String[] args)
{
new MyClient().ClientStart();
}
public void ClientStart()
{
try
{
client=new Socket("192.168.2.101",30000);
System.out.println("已连接");
dos=new DataOutputStream(client.getOutputStream());
dis=new DataInputStream(client.getInputStream());
transmit(new File("e:\\1\\"));
String s="/]00";
byte b[]=s.getBytes();
dos.write(b,0,s.length());
dos.flush();
}
catch(IOException e)
{
System.out.println("Error");
}
}
public void transmit(File f)throws IOException
{
byte b[];
String ts;
int ti;
for(File f1:f.listFiles())
{
if(f1.isDirectory())
{
ts="/]0f"+(f1.getPath().replace("e:\\1\\",""));
b=ts.getBytes();
dos.write(b);
dos.flush();
dis.read();
transmit(f1);
}
else
{
fis=new FileInputStream(f1);
ts="/]0c"+(f1.getPath().replace("e:\\1\\",""));
b=ts.getBytes();
dos.write(b);
dos.flush();
dis.read();
dos.writeInt(fis.available());
dos.flush();
dis.read();
b=new byte[10000];
while(fis.available()>0)
{
ti=fis.read(b);
dos.write(b,0,ti);
dos.flush();
}
dos.flush();
fis.close();
dis.read();
}
}
}
}
import java.io.*;
import java.net.*;
public class MyServer
{
ServerSocket server=null;
Socket client=null;
boolean flag=true;
DataInputStream dis;
DataOutputStream dos;
FileOutputStream fos;
public static void main(String[] args)
{
new MyServer().ServerStart();
}
public void ServerStart()
{
try
{
server=new ServerSocket(30000);
System.out.println("端口号:"+server.getLocalPort());
client=server.accept();
System.out.println("连接完毕");
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
String answer="g";
byte ans[]=answer.getBytes();
byte b[]=new byte[1000];
int ti;
new File("e:\\2").mkdirs();
while(flag)
{
ti=dis.read(b);
dos.write(ans);
String select=new String(b,0,ti);
if(select.contains("/]0f"))
{
File f=new File("e:\\2\\"+(select.replace("/]0f","")));
System.out.println("creat directory");
f.mkdirs();
}
else if(select.contains("/]0c"))
{
fos=new FileOutputStream("e:\\2\\"+(select.replace("/]0c","")));
String cs;
boolean cflag=true;
int tip=dis.readInt();
dos.write(ans);
while(tip>0)
{
ti=dis.read(b,0,(tip>1000?1000:tip));
tip=tip-ti;
cs=new String(b,0,4);
fos.write(b,0,ti);
}
fos.flush();
fos.close();
dos.write(ans);
}
else if(select.contains("/]00"))
{
flag=false;
}
}
dis.close();
client.close();
server.close();
}
catch(IOException e)
{
System.out.println("Error");
}
}
}