import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; /** * Manipulate files in shared folders */ public class ShareFolderUtils { //日志 private static final Logger logger = LoggerFactory.getLogger (ShareFolderUtils. Class ); public final static String username = ""; public final static String password = ""; public final static String sharefolder = ""; // like : ip/share/ public static void main(String[] args) { /* try { List<String> fileNames = getFileNames(username, password, sharefolder); for (String file : fileNames) { System.out.println(file); } } catch (Exception e) { e.printStackTrace (); } */ /* String filePath = sharefolder + "test.txt"; String destination = "D:\\test"; String localfilepath = ShareFolderUtils.loadFile(username, password, filePath, destination); System.out.println(localfilepath); */ } /** * Download files in shared folders * * @param username username * @param password password-free * @param filePath shared file format: ip/share/test.txt * @param destination local storage path format: D:\\test * @return */ public static String loadFile(String username, String password, String filePath, String destination) { logger.info(String.format("start load share file %s .", filePath)); long start = System.currentTimeMillis(); InputStream in = null; OutputStream out = null ; try { // Create a remote file object String url = String.format("smb://%s:%s@%s" , username, password, filePath); SmbFile remoteFile = new SmbFile(url); // try to connect remoteFile.connect(); // Create a file stream in = new BufferedInputStream( new SmbFileInputStream(remoteFile)); String fileName = filePath.substring(filePath.lastIndexOf("/") + 1); String localFilePath = destination + File.separator + fileName; out = new FileOutputStream(new File(localFilePath)); // read file content byte [] buffer = new byte [1024 ]; int len = 0 ; while ((len = in.read(buffer, 0, buffer.length)) != -1 ) { out.write(buffer, 0, len); } // flush the buffered output stream out.flush(); long seconds = (System.currentTimeMillis() - start) / 1000; logger.info(String.format("end load share file (%s) . use time (%d) seconds.", filePath, seconds)); return localFilePath; } catch (Exception e) { logger.error(e.getMessage(), e); } finally { // Close the stream closeStream(in, out); } return null; } /** * Query what files are in the shared folder * * @param username username * @param password password * @param sharefolder Shared folder format: ip/share/ */ public static List<String> getFileNames(String username, String password, String sharefolder) throws Exception { List<String> fileNames = new ArrayList<String>(); try { String url = String.format("smb://%s:%s@%s", username, password, sharefolder); SmbFile file = new SmbFile(url); if (file.exists()) { SmbFile[] files = file.listFiles(); for (SmbFile f : files) { fileNames.add(f.getName()); } } } catch (Exception e) { e.printStackTrace (); logger.error(e.getMessage(), e); } return fileNames; } /** * Close the input and output streams */ public static void closeStream(InputStream in, OutputStream out) { if (null != in) { try { in.close(); } catch (IOException e) { e.printStackTrace (); } } if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace (); } } } }