操作共享文件夹中的文件

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>

posted @ 2018-04-25 15:39  迷失之路  阅读(664)  评论(0编辑  收藏  举报