阅读 62

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

原文:https://blog.51cto.com/u_3764469/2943168

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐