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