java smb 删除_SMB协议、FTP协议方式下载及删除远程服务器文件

package com.voicecyber.thailandsubway.smb;

import com.voicecyber.thailandsubway.utils.ApplicationConstants;

import com.voicecyber.thailandsubway.utils.SystemConfig;

import com.voicecyber.thailandsubway.utils.XmlUtils;

import jcifs.smb.NtlmPasswordAuthentication;

import jcifs.smb.SmbFile;

import jcifs.smb.SmbFileInputStream;

import org.dom4j.Element;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.io.*;

import java.util.List;

/**

* @Author: YafengLiang

* @Description:

* @Date: Created in 11:48 2018/12/7

*/

public class SmbTest {

private static final Logger logger = LoggerFactory.getLogger(SmbTest.class);

public static void main(String[] args) {

MyRun myRun = new MyRun();

new Thread(myRun).start();

}

/**

* 实时同步共享目录文件到本地

*/

public static class MyRun implements Runnable {

@Override

public void run() {

String rootFile = SystemConfig.FileLocation.baseLocation+ ApplicationConstants.FileName.THAILAND_SUBWAY;

Element rootElement = XmlUtils.getRootNode(rootFile);

List list = XmlUtils.getChildElements(rootElement,"smb");

String id1 = "1";

if (list.size()>0){

for (Element element : list) {

String ids = element.attributeValue("id");

if (id1.equals(ids)){

String HOST = element.attributeValue("ip");

String USERNAME = element.attributeValue("username");

String PASSWORD = element.attributeValue("password");

String PATH = element.attributeValue("path");

String LOCAL_DIRECTORY = element.attributeValue("localPath");

String URL = "smb://"+HOST+PATH;

while (true) {

try {

logger.info("十秒后开始。。。。。。");

Thread.sleep(10 * 1000);

logger.info("继续。。。。。。");

getRemoteFile(URL,USERNAME,PASSWORD,LOCAL_DIRECTORY,HOST);

} catch (InterruptedException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

}

}

}

/**

* 获取远程文件

* @param remoteUsername 远程目录访问用户名

* @param remotePassword 远程目录访问密码

* @param url 远程文件地址,该参数需以IP打头,如'192.168.6.121/aa/bb.java'或者'192.168.6.121/aa/',如'192.168.6.121/aa'是不对的

* @param localDirectory 本地存储目录,该参数需以'/'结尾,如'D:/'或者'D:/mylocal/'

* @return boolean 是否获取成功

* @throws Exception

*/

public static boolean getRemoteFile(String url,String remoteUsername, String remotePassword,

String localDirectory,String HOST) throws Exception {

boolean isSuccess = Boolean.FALSE;

if (url.startsWith("/") || url.startsWith("\\")) {

return isSuccess;

}

if (!(localDirectory.endsWith("/") || localDirectory.endsWith("\\"))) {

return isSuccess;

}

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(HOST,remoteUsername,remotePassword);

SmbFile smbFile = new SmbFile(url, auth);

if (smbFile != null) {

if (smbFile.isDirectory()) {

SmbFile[] smbFiles = smbFile.listFiles();

for (SmbFile file : smbFiles) {

File file1 = new File(localDirectory+File.separator+file.getName());

if (smbFile.isDirectory()) {

getRemoteFile(url + File.separator + file.getName(), remoteUsername, remotePassword, localDirectory + File.separator + file.getName(),HOST);

if (!file1.exists()) {

isSuccess = copyFile(file, localDirectory);

}else {

logger.info("已存在 "+file1);

String smbUrl = url + File.separator + file.getName();

String str = smbUrl.replace("\\\\","");

SmbFile smbFile1 = new SmbFile(str,auth);

smbFile1.delete();

logger.info("删除:"+smbUrl.replace("\\\\","")+" 成功");

}

}else {

isSuccess = copyFile(file, localDirectory);

}

}

} else if (smbFile.isFile()) {

isSuccess = copyFile(smbFile, localDirectory);

}

}

return isSuccess;

}

/**

* 拷贝远程文件到本地目录

* @param smbFile 远程SmbFile

* @param localDirectory 本地存储目录,本地目录不存在时会自动创建,本地目录存在时可自行选择是否清空该目录下的文件,默认为不清空

* @return boolean 是否拷贝成功

*/

public static boolean copyFile(SmbFile smbFile, String localDirectory) {

SmbFileInputStream in = null;

FileOutputStream out = null;

try {

File[] localFiles = new File(localDirectory).listFiles();

if (null == localFiles) {

// 目录不存在的话,就创建目录

new File(localDirectory).mkdirs();

logger.info(localDirectory+"目录创建成功!");

}

in = new SmbFileInputStream(smbFile);

out = new FileOutputStream(localDirectory + smbFile.getName());

byte[] buffer = new byte[4096];

int len = -1;

while ((len = in.read(buffer)) != -1) {

out.write(buffer, 0, len);

buffer = new byte[4096];

}

out.flush();

logger.info("拷贝 "+smbFile.getName()+" 成功");

} catch (Exception e) {

logger.error("拷贝远程文件到本地目录失败", e);

return false;

} finally {

if (null != out) {

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (null != in) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return true;

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值