zoukankan      html  css  js  c++  java
  • 操作共享文件夹中的文件

    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();
                }
            }
        }
    
    }

     依赖jar

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

  • 相关阅读:
    springmvc 项目完整示例08 前台页面以及知识点总结
    springmvc 项目完整示例07 设置配置整合springmvc springmvc所需jar包springmvc web.xml文件配置
    springmvc 项目完整示例06 日志–log4j 参数详细解析 log4j如何配置
    springmvc 项目完整示例05 日志 --log4j整合 配置 log4j属性设置 log4j 配置文件 log4j应用
    springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用
    springmvc 项目完整示例03 小结
    springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试
    springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目
    spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例
    spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包
  • 原文地址:https://www.cnblogs.com/zj0208/p/8945165.html
Copyright © 2011-2022 走看看