Java SFTP指定绝对路径

菲利普·克劳斯(Filip Kraus)

我已将此代码用于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 /选择路径

当连接到服务器但连接到

/选择路径

使用此部分代码时:

   //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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Java Nio相对路径的绝对路径

相对路径与绝对路径Java

带有gradle的Java 11:必须指定绝对路径,但必须为$ {tools.jar}

在Java中绝对路径打开文件

获取java中URL的绝对路径

如何在Java中获取文件的绝对路径

如何从InputStream获取Java文件的绝对路径?

Java中项目文件夹的绝对路径

Java找到依赖jar的绝对路径

如何在Java中获取绝对路径

通过绝对路径在Java中导入类

Java使用相对路径而不是绝对路径

Java:无论操作系统如何,如何确定路径是否为绝对路径

如何在不指定绝对路径的情况下从另一个Java包中读取文件(例如txt文件)?

如何用两个绝对路径(或URL)在Java中构造一个相对路径?

资源文件和Java类的Javascript绝对路径

Java nio:如何将扩展添加到绝对路径?

从Java类文件获取apache webcontents文件夹的绝对路径

如何在Java中使用正确的字符编码获取绝对路径?

获取Java中“ Web内容”文件夹的绝对路径

具有绝对路径的Java FileNotFoundException-无法读取或执行,但是文件存在

在java中获取绝对路径的最后一个元素是否更好?

不使用绝对路径用java创建文本文件

如何让我的java项目在不使用绝对路径的情况下使用某些文件?

如何使用jfilechooser从Java中的文件名数组中获取多个文件选择的绝对路径

Java无法打开正确的文件,即使使用绝对路径,也会不断返回File Not Found异常

java.lang.IllegalArgumentException:无效的uri'/ Assignment / rest / words / {name} / protection':转义的绝对路径无效

java.nio.file.WatchEvent仅提供相对路径。如何获取修改后的文件的绝对路径?

Snakemake:如何指定shell命令的绝对路径