\(^_^)/ Java 使用Socket多线程传输整个文件夹

一.列出莫文件夹中所有的文件和文件夹的工具类,磁盘列表类

 

package upload;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ListFiles {
	private List<File> fileList = new ArrayList<File>();

	public void listAllFiles(File file) {
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				if (files[i].listFiles() != null && files[i].listFiles().length > 0) {
					listAllFiles(files[i]);
				} else {
					fileList.add(files[i]);
				}
			} else {
				fileList.add(files[i]);
			}
		}
	}

	public List<File> getAllFiles(File file) {
		this.listAllFiles(file);
		return fileList;
	}

	public static void main(String[] args) {
		System.out.println(new ListFiles().getAllFiles(new File("E:\\temp\\")));
	}
}

 

package upload;

public enum DiskName {
	C, D, E, F, G, H, I, J, K
}

 

 

 

 

 

二.服务器端程序

1.文件处理

package upload;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;

public class UploadFileServer implements Runnable {

	private Socket socket;
	private DiskName diskName;

	public UploadFileServer(Socket socket, DiskName diskName) {
		this.socket = socket;
		this.diskName = diskName;
	}

	@Override
	public void run() {
		DataInputStream dis = null;
		FileOutputStream fos = null;

		try {
			long start = System.currentTimeMillis();
			dis = new DataInputStream(socket.getInputStream());
			String filePath = dis.readUTF();
			String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
			String strDir = diskName.name()
					+ filePath.substring(filePath.indexOf(":"), filePath.lastIndexOf("\\"))
					+ File.separator;

			File d = new File(strDir);
			d.mkdirs();

			File file = new File(strDir + fileName);
			file.createNewFile();

			if (file.isFile()) {
				fos = new FileOutputStream(file);
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = dis.read(buffer, 0, buffer.length)) > 0) {
					fos.write(buffer, 0, len);
					fos.flush();
				}
			}

			long end = System.currentTimeMillis();
			System.out.println("cost: " + ((end - start) / 1000) + " s, size: "
					+ (file.length() / 1024.0) + " KB , file: " + file.getAbsolutePath());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

 

2.启动类

package upload;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(9999);
		while (true) {
			Socket socket = server.accept();
			new Thread(new UploadFileServer(socket, DiskName.D)).start();
		}
	}
}

 

 

 

 

 

 

三.客户端程序

1.文件处理

package upload;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;

public class UploadFileClient implements Runnable {
	private Socket socket;
	private File file;

	public UploadFileClient(Socket socket, File file) {
		this.socket = socket;
		this.file = file;
	}

	@Override
	public void run() {
		DataOutputStream dos = null;
		FileInputStream fis = null;
		try {
			long start = System.currentTimeMillis();
			dos = new DataOutputStream(socket.getOutputStream());
			if (file.isFile()) {
				fis = new FileInputStream(file);

				dos.writeUTF(file.getAbsolutePath());
				byte[] sendBuffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(sendBuffer, 0, sendBuffer.length)) > 0) {
					dos.write(sendBuffer, 0, len);
					dos.flush();
				}
			} else {
				dos.writeUTF(file.getAbsolutePath() + File.separator);
			}
			long end = System.currentTimeMillis();
			System.out.println("cost: " + ((end - start) / 1000) + " s, size: "
					+ (file.length() / 1024.0) + " KB , file: " + file.getAbsolutePath());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (dos != null) {
				try {
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (socket != null) {
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

 

 

2.启动类

package upload;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;

public class SocketClient {
	public static void main(String[] args) throws IOException {
		List<File> fileList = new ListFiles().getAllFiles(new File("E:\\java standard\\"));
		for (int i = 0; i < fileList.size(); i++) {
			Socket socket = new Socket();
			socket.connect(new InetSocketAddress("127.0.0.1", 9999));
			new Thread(new UploadFileClient(socket, fileList.get(i))).start();
		}
	}
}

 

你可能感兴趣的:(socket)