首页 > 代码库 > 文件秒传

文件秒传

image

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("\t正在校验 " + file.getName() + "文件MD5请稍后...\n");			String md5 = MD5FileUtil.getFileMD5String(file);// 把此文件数据进行MD5加密 System.out.println("[MD5验证完毕  耗时: " + 			(System.currentTimeMillis() - times) / 1000 + " 秒  正在执行数据传输]\n");			fout.write(md5.getBytes());// 把加密出来的MD5发送到服务端 fout.flush(); if (fin.read() == 0) {// 服务端传过来的是0 说明服务端上有相同数据 // 打印秒传了 System.out.println("数据传输完毕!大小: " + file.length() + " 字节  耗时: 0秒 ! 秒传机制");			} else { // 打印的不是0,那么开始把数据传输给服务端 long time = System.currentTimeMillis();				System.out.println("\t\t数据传输中...\n");				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);//接收客户端传的MD5 System.out.println(md5);			File file = new File("filess");			File[] files = file.listFiles(); boolean state = false;//状态默认为false 用来判断MD5是否存在 for (File file2 : files) { //如果找到了相同的MD5 if (file2.getName().equalsIgnoreCase(md5)) {					state = true;//状态就改为true 表示有相同的MD5 break;				}			} if (state == false) { //没有找到相同的MD5 就给客户端发送1过去 让客户端开始把数据发过来 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 { //如果找到了相同的MD5那么发送0给客户端  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();		}	}}

文件秒传