博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
操作共享文件夹中的文件
阅读量:6905 次
发布时间:2019-06-27

本文共 4401 字,大约阅读时间需要 14 分钟。

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
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
getFileNames(String username, String password, String sharefolder) throws Exception { List
fileNames = new ArrayList
(); 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(); } } }}

 

 依赖jar

<dependency>

  <groupId>org.samba.jcifs</groupId>
  <artifactId>jcifs</artifactId>
  <version>1.2.19</version>
</dependency>

转载地址:http://asldl.baihongyu.com/

你可能感兴趣的文章
第1章 游戏之乐——NIM(3)两堆石头的游戏
查看>>
eclipse中新建python项目报错:Project interpreter not specified
查看>>
如何在Linux上实现文件系统的自动检查和修复?
查看>>
jquery ajax调用返回json格式数据处理
查看>>
奥姆卡剃刀原理
查看>>
数据结构(C实现)------- 单链表
查看>>
ORA-28000: the account is locked-的解决办法
查看>>
大型网站架构的演化
查看>>
(笔记)电路设计(十一)之DC/DC电源转换方案设计应用
查看>>
Cannot complete the install because one or more required items could not be found
查看>>
关于使用chrome插件改动全部的站点的响应responseHeaders头的注意
查看>>
hibernate下载包中配置文件路径
查看>>
项目命名规则
查看>>
C语言 · 字符串输入输出函数
查看>>
css中单位em和rem
查看>>
C#编程(二十一)----------扩展方法
查看>>
BZOJ 2141: 排队 [CDQ分治]
查看>>
netty5入门教程
查看>>
SecureCRT 连接虚拟机Linux
查看>>
你是否也忘了刷新视图?
查看>>