gpt4 book ai didi

Java SFTP指定绝对路径

转载 作者:行者123 更新时间:2023-12-02 04:38:00 27 4
gpt4 key购买 nike

我已使用此代码进行 SFTP Java 上传

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 the file exists
String filepath = localDirectory + fileToFTP;
File file = new File(filepath);
if (!file.exists())
throw new RuntimeException("Error. Local file not found");

//Initializes the 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 the SFTP URI using the host name, userid, password, remote path and 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 to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
System.out.println("File upload successful");

}
catch (Exception ex) {
ex.printStackTrace();
return false;
}
finally {
manager.close();
}

return true;
}


}

代码来源是:http://www.mysamplecode.com/2013/06/sftp-apache-commons-file-download.html

我需要指定路径,以便 ftp 无法登录

/root/CHOSENPATH

当连接到服务器时

/CHOSENPATH

使用这部分代码时:

   //Create the SFTP URI using the host name, userid, password,  remote path and file name
String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" +
remoteDirectory + fileToFTP;

有没有办法指定绝对路径而不是相对路径?

提前谢谢您。

最佳答案

您的代码中有这一行:

SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);

我会尝试将该标志设置为 false。

SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, false);

关于Java SFTP指定绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30531663/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com