Java SFTP specify absolute path -
i have used code sftp java upload
package com.as400samplecode; import java.io.file; import java.io.fileinputstream; import java.util.properties; import org.apache.commons.vfs2.fileobject; import org.apache.commons.vfs2.filesystemoptions; import org.apache.commons.vfs2.selectors; import org.apache.commons.vfs2.impl.standardfilesystemmanager; import org.apache.commons.vfs2.provider.sftp.sftpfilesystemconfigbuilder; public class sendmyfiles { static properties props; public static void main(string[] args) { sendmyfiles sendmyfiles = new sendmyfiles(); if (args.length < 1) { system.err.println("usage: java " + sendmyfiles.getclass().getname()+ " properties_file file_to_ftp "); system.exit(1); } string propertiesfile = args[0].trim(); string filetoftp = args[1].trim(); sendmyfiles.startftp(propertiesfile, filetoftp); } public boolean startftp(string propertiesfilename, string filetoftp){ props = new properties(); standardfilesystemmanager manager = new standardfilesystemmanager(); try { props.load(new fileinputstream("properties/" + propertiesfilename)); string serveraddress = props.getproperty("serveraddress").trim(); string userid = props.getproperty("userid").trim(); string password = props.getproperty("password").trim(); string remotedirectory = props.getproperty("remotedirectory").trim(); string localdirectory = props.getproperty("localdirectory").trim(); //check if file exists string filepath = localdirectory + filetoftp; file file = new file(filepath); if (!file.exists()) throw new runtimeexception("error. local file not found"); //initializes file manager manager.init(); //setup our sftp configuration filesystemoptions opts = new filesystemoptions(); sftpfilesystemconfigbuilder.getinstance().setstricthostkeychecking( opts, "no"); sftpfilesystemconfigbuilder.getinstance().setuserdirisroot(opts, true); sftpfilesystemconfigbuilder.getinstance().settimeout(opts, 10000); //create sftp uri using host name, userid, password, remote path , file name string sftpuri = "sftp://" + userid + ":" + password + "@" + serveraddress + "/" + remotedirectory + filetoftp; // create local file object fileobject localfile = manager.resolvefile(file.getabsolutepath()); // create remote file object fileobject remotefile = manager.resolvefile(sftpuri, opts); // copy local file sftp server remotefile.copyfrom(localfile, selectors.select_self); system.out.println("file upload successful"); } catch (exception ex) { ex.printstacktrace(); return false; } { manager.close(); } return true; } }
source of code is: http://www.mysamplecode.com/2013/06/sftp-apache-commons-file-download.html
i need specify path ftp not log into
/root/chosenpath
when connected server to
/chosenpath
when using part of code:
//create sftp uri using host name, userid, password, remote path , file name string sftpuri = "sftp://" + userid + ":" + password + "@" + serveraddress + "/" + remotedirectory + filetoftp;
is there way how specify absolute path instead of relative?
thank in advance.
you have line in code:
sftpfilesystemconfigbuilder.getinstance().setuserdirisroot(opts, true);
i try set flag false.
sftpfilesystemconfigbuilder.getinstance().setuserdirisroot(opts, false);
Comments
Post a Comment