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<Element> 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; } }

598381336 LV1
2024年3月29日
一蓑烟雨 LV11
2021年4月2日
lironggang LV38
2021年2月5日
guaixia163 LV13
2020年11月11日
ChenTong_ZuiDaiMa LV2
2020年10月2日
lyd19931203 LV21
2020年6月2日
15838634741 LV18
2020年4月2日
qq652133917 LV1
2020年1月11日
寒江无月 LV1
2019年11月7日
南风你好吗 LV5
2019年9月30日