• 操作共享文件夹中的文件


    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>

  • 相关阅读:
    C#泛型使用小记
    Unity3d + NGUI 的多分辨率适配
    CodeSmith(1 生成实体)
    从数据库读取数据Table后转成对应的实体泛型方法
    安卓data./data没数据的时候
    Java工具
    SqlBulkCopy 用法
    Wind7 64位配置安卓环境
    .net和c#下的自定义配置
    Log4Net可以根据不同的类容输出到不同的文件夹下面
  • 原文地址:https://www.cnblogs.com/zj0208/p/8945165.html
Copyright © 2020-2023  润新知