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; /** * 操做共享文件夾中的文件 */ 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); */ } /** * 下載共享文件夾中的文件 * * @param username 用戶名 * @param password 免密 * @param filePath 共享文件 格式:ip/share/test.txt * @param destination 本地存儲路徑 格式: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 { //建立遠程文件對象 String url = String.format("smb://%s:%s@%s", username, password, filePath); SmbFile remoteFile = new SmbFile(url); //嘗試鏈接 remoteFile.connect(); //建立文件流 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)); //讀取文件內容 byte[] buffer = new byte[1024]; int len = 0; while ((len = in.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } //刷新緩衝的輸出流 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 { // 關閉流 closeStream(in, out); } return null; } /** * 查詢共享夾子下有哪些文件 * * @param username 用戶名 * @param password 密碼 * @param sharefolder 共享文件夾 格式: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; } /** * 關閉輸入輸出流 */ 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(); } } } }
依賴jarjava
<dependency>
<groupId>org.samba.jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.2.19</version>
</dependency> url