Java通过SSH实现文件上传下载功能实现之二主程序

接着上一遍,主程序的逻辑比较复杂,所以我将主程序单独来讲解一下。

 

1.以zip方式上传文件夹到服务器

业务逻辑:压缩对应的文件夹,生成临时文件.zip  ---->上传临时文件.zip,上传成功,删除本地临时文件  
                  ----> 远程服务上解压到服务器指定路径,并删除服务器上临时zip文件 ---->删除配置文件information.txt

1.解析配置文件information.txt的内容,获得需要压缩的本地文件夹

//1.先压缩指定文件夹为zip
FileOutputStream fileOutputStream = new FileOutputStream(new File(localZipFilePath));
ZipUtils.toZip(config.getLocalFilePath(), fileOutputStream,true);

2.上传本地的压缩包

SSHWindows.scpPut(config.getAddress(), config.getServerUserName(),
		       config.getServerPassword(), localZipFilePath,
		       config.getServerSavePath());

3.成功上传本地压缩包后,删除本地压缩包

File removeFile = new File(localZipFilePath);
try{
    removeFile.delete();
}catch(Exception e){
	System.out.println("删除本地压缩文件异常!");
}

4.校验服务器是否存在要被解压的文件夹名称,若已存在,先删除服务器上同名的文件夹

int lastIndex = config.getLocalFilePath().lastIndexOf("\\");
String lastLocalDir = config.getLocalFilePath().substring(lastIndex + 1);
if(lastLocalDir.equals(config.getServerSaveFileName())) {
	String deleteSourceDirCommand = "rmdir /s/q "+ config.getServerSavePath()+"\\" + 
    config.getServerSaveFileName();
    sshWindows.execConmand(config, deleteSourceDirCommand);
}

这里说明一下:java调用windows的命令  rmdir /s/q d:\ftpServer\test2  ,文件夹的路径以“\”分隔,否则会报错

5.解压服务器上的压缩包到指定路径

解压操作也是需要执行windows的命令,winrar x d:\ftpServer\test2.zip  -r  d:\ftpServer\test3

注:这里需要先要将WinRAR软件设置到环境变量。

6.删除服务器上的压缩包

windows的删除命令:del /f/s/q  E:\test\test1  

7.最后删除配置文件information.txt

public static void deleteConfigFile() {
	File file = new File(configPath);
	if(file.exists()) {
		boolean deleted = file.delete();
		System.out.println("delete "+configPath +" sucessed");
	}
}<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不安分的猿人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值