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

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

package upload;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class ListFiles {

private List fileList = new ArrayList();

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 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 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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值