1. 程式人生 > >Apache commons-vfs2

Apache commons-vfs2

vfs java

依賴:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-vfs2 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-vfs2</artifactId>
        <version>2.2</version>
    </dependency>

    <!--當需要使用commons-vfs的sftp功能用-->
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.53</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.9</version>
    </dependency>

</dependencies>

測試類:

import org.apache.commons.io.IOUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.io.File;

/**
 * @author by lei zhou on 2017/11/13 11:15.
 */
public class Tester {
    public static void main(String[] args) {

        resolveFile("res:1/11111");

        resolveFile("zip://" + System.getProperty("user.dir") + File.separator + "c.zip");

        resolveFile("file://" + System.getProperty("user.dir") + File.separator + "Java9Test.iml");

        resolveFile("sftp://test:[email protected]:22/home/zhoulei");
    }

    public static void resolveFile(String path) {
        try {
            FileSystemManager fsManager = VFS.getManager();

            FileSystemOptions opts = new FileSystemOptions();

            if (path.startsWith("sftp:")) {
                // 設false時,URI要傳絕對路徑,設true時,URI傳相對於遠程用戶根目錄的相對路徑
                SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);
                SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
                SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
            }

            FileObject fileObject = fsManager.resolveFile(path, opts);

            if (fileObject.isFolder()) {
                FileObject[] childs = fileObject.getChildren();

                for (FileObject child : childs) {
                    print(child);
                }
            } else {
                print(fileObject);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void print(FileObject fileObject) throws Exception {
        System.out.println(fileObject.getPublicURIString() + " " + getFormatedDateTime(fileObject.getContent().getLastModifiedTime()) + " " + IOUtils.toString(fileObject.getContent().getInputStream(), "UTF-8"));
    }

    private static String getFormatedDateTime(long millis) {
        return (new DateTime(millis)).toString(DateTimeFormat.fullDateTime());
    }
}

項目結構:

技術分享

Apache commons-vfs2