netty实现文件同步(四)

fscserver这个程序是接收fscclient发送的文件。主要程序如下:

@Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        if (msg instanceof TransFile) {
            TransFile ef = (TransFile) msg;
            InetSocketAddress insocket = (InetSocketAddress) ctx.channel()
                    .remoteAddress();
            String clientIP = insocket.getAddress().getHostAddress();
            String dir = filepath + File.separator + clientIP + File.separator + ef.getPath();
            if (ef.getCode() == CodeEnum.ADDFILE.getCode()) {
                byte[] bytes = ef.getBytes();
                len = ef.getEnd();
                String md5 = ef.getMd5();
                createFolder(ef,dir);
                if (start == 0) {
                    String path = dir + File.separator
                            + ef.getName();
                    file = new File(path);
                    fileSize = ef.getSize();
                    if (file.exists() && !file.isDirectory()) {
                        InputStream is = new FileInputStream(file);
                        String ymd5 = DigestUtils.md5Hex(is);
                        is.close();
                        if (md5.equals(ymd5)) {
                            BackInfo backInfo = new BackInfo(start);
                            ctx.writeAndFlush(backInfo);
                            return;
                        }
                    }else{
                        if(!"".equals(file.getParent())) {
                            File fileParent = new File(file.getParent());
                            if(!fileParent.exists()) {
                                fileParent.mkdirs();
                            }
                        }
                    }

                    randomAccessFile = new RandomAccessFile(file, "rw");
                }
                randomAccessFile.seek(start);
                randomAccessFile.write(bytes);
                start = start + len;

                if (len > 0 && (start < fileSize && fileSize != -1)) {
                    BackInfo backInfo = new BackInfo(start, md5, (start * 100)
                            / fileSize);
                    ctx.writeAndFlush(backInfo);
                } else {
                    log.info("create file success:" + ef.getName());

                    BackInfo backInfo = new BackInfo(start);
                    ctx.writeAndFlush(backInfo);
                    randomAccessFile.close();
                    file = null;
                    fileSize = -1;
                    randomAccessFile = null;
                }
            } else if (ef.getCode() == CodeEnum.DELFILE.getCode()) {
                String path = dir
                        + File.separator + ef.getName();
                file = new File(path);
                if(file.exists()) {
                    String backpath = backfilepath + File.separator + clientIP + File.separator + ef.getPath();
                    File backfiles = new File(backpath);
                    if (!backfiles.exists()) {
                        backfiles.mkdirs();
                    }
                    File backfile = new File(backpath + File.separator + ef.getName());
                    Files.copy(file.toPath(), backfile.toPath(),StandardCopyOption.COPY_ATTRIBUTES,StandardCopyOption.REPLACE_EXISTING);
                    boolean b = file.delete();
                    if (!b) {
                        log.error("del file error:" + path);
                    }
                }
                BackInfo backInfo = new BackInfo(start);
                ctx.writeAndFlush(backInfo);
            }
        }
    }

还有对外的接口,需要在head中传入Authorization,默认用户和密码都是root。
fileall接口就是全量文件以json进行返回,包括文件地址和md5。filebyfold接口是以json格式返回某个文件夹的文件信息。filebyfold接口是以json格式返回某个文件夹的文件信息。down接口可以从服务器下载文件

配置

    server.filepath=上传路径
    server.backfilepath=删除后备份路径
    server.backtime=删除后几天彻底删除
    server.port=端口
    server.author=http的Authorization

打包

把fscserver文件夹打包成一个可执行的jar,main是FscServerMain,config.properties配置文件放到和jar同一个目录。main内容如下

public static void main(String[] args) {
        int port = PropertiesUtils.getIntValue(Content.PORT);
        if (args != null && args.length > 0) {
            try {
                port = Integer.valueOf(args[0]);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        new Thread(() -> {
            try {
                Timer timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask() {
                    public void run() {
                        FileUtils.getFileList(backfilepath, backtime);
                    }
                }, 0, 1000 * 60 * 60 * 24);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        ).start();
        new FscServerMain().start();
    }


具体代码详见
    Github地址  https://github.com/zzxiaoma163/fsc
    gitee地址  https://gitee.com/XiaoMaGeGeW/fsc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值