Apache commons-vfs2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache commons-vfs2相关的知识,希望对你有一定的参考价值。

依赖:

<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的主要内容,如果未能解决你的问题,请参考以下文章

Apache VFS 错误。因为它是相对路径,并且没有提供基本 URI

kettle问题

环境初始化 Build and Install the Apache Thrift IDL Compiler Install the Platform Development Tools(代码片段

spark关于join后有重复列的问题(org.apache.spark.sql.AnalysisException: Reference '*' is ambiguous)(代码片段

如何在gradle文件夹中下载带有pom文件的jar文件?

apache_conf Linux片段