Depend on:
<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> <!-- When you need to use Commons-vfs's sftp function with--> <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>
Test class:
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") +&nbsP 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:")) { // set False when the URI is to pass the absolute path, set True when The relative path of the URI relative to the remote user root directory 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 ()); }}
Project structure:
650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/13/931b8af6b367859dcd07f3d328a0241c.png-wh_500x0-wm_3 -wmp_4-s_3664611781.png "title=" Qq20171113170638.png "alt=" 931b8af6b367859dcd07f3d328a0241c.png-wh_ "/>
Apache COMMONS-VFS2