码迷,mamicode.com
首页 > Web开发 > 详细

Apache commons-vfs2

时间:2017-11-13 18:29:23      阅读:607      评论:0      收藏:0      [点我收藏+]

标签: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:test@192.168.1.111: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

标签:vfs   java   

原文地址:http://zl1030.blog.51cto.com/274507/1981324

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!