ClientFiles.java
/**
* 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com
* @author 小沫
*/
package com.files.mc;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientFiles extends Thread {
public void run() {
try {
Socket socket = new Socket("127.0.0.1", 8080);
InputStream fin = socket.getInputStream();
OutputStream fout = socket.getOutputStream();
File file = new File("E:/迅雷下载/170322-04.mp4");
long times = System.currentTimeMillis();
System.out.println(" 正在校验 " + file.getName() + "文件MD5请稍后...
");
String md5 = MD5FileUtil.getFileMD5String(file);
System.out.println("[MD5验证完毕 耗时: " +
(System.currentTimeMillis() - times) / 1000 + " 秒 正在执行数据传输]
");
fout.write(md5.getBytes());
fout.flush();
if (fin.read() == 0) {
System.out.println("数据传输完毕!大小: " + file.length() + " 字节 耗时: 0秒 ! 秒传机制");
} else {
long time = System.currentTimeMillis();
System.out.println(" 数据传输中...
");
FileInputStream filein = new FileInputStream(file);
byte[] b = new byte[1024 * 1024 * 2];
while (filein.available() != 0) {
int len = filein.read(b);
fout.write(b, 0, len);
fout.flush();
}
System.out.println(
"文件传输完毕!大小: " + file.length() + " 字节 耗时: " + (System.currentTimeMillis() - time) / 1000 + "秒");
filein.close();
}
fout.close();
fin.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ClientFiles().start();
}
}
ServerFiles.java
/**
* 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com
* @author 小沫
*/
package com.files.mc;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.InflaterOutputStream;
public class ServerFiles implements Runnable {
private Socket socket;
public ServerFiles(Socket socket) {
this.socket = socket;
}
public void run() {
try {
InputStream fin = socket.getInputStream();
OutputStream fout = socket.getOutputStream();
byte[] b = new byte[1024];
int len = 0;
len = fin.read(b);
String md5 = new String(b, 0, len);
System.out.println(md5);
File file = new File("filess");
File[] files = file.listFiles();
boolean state = false;
for (File file2 : files) {
if (file2.getName().equalsIgnoreCase(md5)) {
state = true;
break;
}
}
if (state == false) {
fout.write(1);
fout.flush();
FileOutputStream fileout = new FileOutputStream(new File("filess", md5));
byte[] b1 = new byte[1024 * 1024 * 2];
while ((len = fin.read(b)) != -1) {
fileout.write(b1, 0, len);
}
fileout.close();
} else {
fout.write(0);
fout.flush();
}
fout.close();
fin.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void openServer() throws Exception {
ServerSocket server = new ServerSocket(8080);
ExecutorService executor = Executors.newFixedThreadPool(10);
while (true) {
ServerFiles socket = new ServerFiles(server.accept());
executor.execute(socket);
}
}
public static void main(String[] args) {
try {
openServer();
} catch (Exception e) {
e.printStackTrace();
}
}
}