服务器端:
package info.dyndns.oszc.TcpServer;
import java.net.*;
import java.io.*;
public class TcpServer implements Runnable {
Socket s;
public TcpServer(Socket val){
s = val;
}
public void run(){
try {
Service();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void Service() throws IOException {
InputStream input = s.getInputStream();
OutputStream w = s.getOutputStream();
byte[] indication = new byte[1024];
int len;
len = input.read(indication);// read
w.write("11".getBytes()); //write 这里所有的写操作是为了 区分读取次序
String indcat = new String(indication, 0, len);
//System.out.println(indcat); // for test
if ("isDirectory".equals(indcat)) {
//System.out.println("in directory"); // for test
byte[] path = new byte[1024];
len = input.read(path); //read
w.write("11".getBytes()); //write
String fileName = new String(path, 0, len);
// fileName.replace("d:\\temp", "D:\\XML"); // for test
String dirName = fileName.replace("D:\\星火英语", "d:\\XML\\Test"); // 把目标的绝对路径父目录换成要存放的目录的父目录
//System.out.println(dirName); //for test
new File(dirName).mkdir(); // creat dir
} else {
//System.out.println("in file"); // for test
byte[] path2 = new byte[1024];
len = input.read(path2); //read
w.write("11".getBytes()); //write
String fileName = new String(path2, 0, len);
// fileName.replace("d:\\temp", "D:\\XML"); // for test
String pathName = fileName.replace("D:\\星火英语", "d:\\XML\\Test"); // 理由同操作 文件夹一样
System.out.println(pathName);
//read 创建文件内容
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pathName)));
byte[] buff = new byte[1024*1024];
while ( (len =input.read(buff))!=-1)
out.write(buff,0,len);
out.close();
}
}
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
ServerSocket ss = new ServerSocket(10001);
while(true){
Socket s =ss.accept();
new Thread(new TcpServer(s)).start();
}
}
}
客户端:
package info.dyndns.oszc.TcpClient;
import java.net.*;
import java.io.*;
/**
*
* 此程序能将带层次的复制文件夹
* 10001端口
* 传送一个文件夹 D://temper
* 得到他的子文件
*
* @author ZC
*
*/
public class TcpClient {
Socket s ;
InetAddress ip;
public TcpClient(){
try {
ip = InetAddress.getByName("192.168.1.8"); //服务器ip地址
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/**
*
* @param infile 需要复制文件夹地址
* @throws UnknownHostException
* @throws IOException
*/
public void service(File infile) throws UnknownHostException, IOException{
File[] files = infile.listFiles();
for (File file:files){
if ((file.isDirectory())){
//发送信号
//System.out.println(file.getName()+"...is directory"); //for test
sendDirectorySignal(file.getAbsolutePath());
//递归
service(file);
continue;
}
//发送文件
sendFileSignal(file.getAbsolutePath());
System.out.println(file.getName() +"..."+ "is file");
}
}
/**
*
* @param msg 文件绝对路径
* @throws IOException
*/
public void sendDirectorySignal(String msg) throws IOException{
Socket socket = new Socket(ip,10001);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
out.write("isDirectory".getBytes());
in.read(); //这里所有的read是为了确保 write 能够分次传输
out.write(msg.getBytes());
out.flush();
out.close();
socket.close();
//System.out.println(msg+"...send!");
}
/**
*
* @param msg 文件绝对路径
* @throws IOException
*/
public void sendFileSignal(String msg) throws IOException{
Socket socket = new Socket (ip, 10001);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
out.write("isFile".getBytes()); //写
in.read(); // 读
//filename?
out.write(msg.getBytes()); //写
in.read();//读
//写
BufferedInputStream input = new BufferedInputStream(new FileInputStream(new File(msg)));
byte[] buff = new byte[1024*1024];
int len;
while ( (len = input.read(buff))!=-1){
out.write(buff,0,len);
}
out.flush();
input.close();
out.close();
socket.close();
}
public static void main(String[] a) throws IOException{
TcpClient t = new TcpClient();
//t.test();
t.service(new File("D:\\星火英语"));
}
}