0

我创建了一个 XML 和一个 ZIP 文件,并通过 SFTP 将它们上传到服务器。文件夹结构如下所示:

/
|
|--/incoming
       |
       |--/<hash>
             |
             |-- file.xml
             |-- file.zip

该文件夹<hash>是在我上传 XML 和 ZIP 时创建的,我需要此文件夹才能拥有权限777

据我所知,我无法通过 Java 中的 VFS 更改已创建文件夹的权限。我当时尝试的是在本地创建该文件夹,777将其与 XML 和 ZIP 一起上传。

我的代码如下所示:

File fUploadDir = new File(uploadDir);
fUploadDir.mkdir();

fUploadDir.setReadable(true, false);
fUploadDir.setWritable(true, false);
fUploadDir.setExecutable(true, false);

// Create and add ZIP and XML files...
// ...

StandardFileSystemManager manager = new StandardFileSystemManager();

// Initializes the file manager
manager.init();

File file = new File(pathToFolder);

// Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

String sftpUri = "sftp://" + userId + ":" + password + "@" + serverAddress + "/" + remoteDirectory;

// Create local file object
FileObject localFile = manager.resolveFile(fUploadDir.getAbsolutePath());

// Create remote file object         
FileObject remoteFile = manager.resolveFile(sftpUri, opts);

// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF_AND_CHILDREN);

当我执行此代码时,将上传 XML 和 ZIP,但不会上传目录,因此 SFTP 服务器上的结构如下所示:

/
|
|--/incoming
       |
       |-- file.xml
       |-- file.zip

我如何才能获得具有权限的文件夹777

4

1 回答 1

1

我设法更改了权限。我的代码如下所示:

StandardFileSystemManager manager = new StandardFileSystemManager();

String serverAddress = Config.getProperty("SFTP.SERVER.URL");
String userId = Config.getProperty("SFTP.SERVER.USERID");
String password = Config.getProperty("SFTP.SERVER.PASSWORD");
String remoteDirectory = Config.getProperty("SFTP.SERVER.REMOTEPATH");

JSch jsch = new JSch();

Session session = jsch.getSession(userId, serverAddress, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");                

session.connect();

Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp cSftp = (ChannelSftp) channel;

// check if the file exists
String filepath = localDirectory + File.separator + fileToFTP;
File file = new File(filepath);
if (!file.exists()) {
  logger.error(filepath + " existiert nicht.");
  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 + "/" + hash + "/" + 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);

// Set file permissions to 777.
// 511 is the decimal representation for octal 777.
cSftp.chmod(511, remoteDirectory + "/" + hash);

如您所见,我仍然使用 VFS,但仅用于文件传输。我已经创建了 ZIP 文件并将其上传到 SFTP 服务器的目录incoming/<hash>中。<hash>如果该目录尚不存在,VFS 将创建该目录。<hash>文件上传后,我使用.更改目录的文件权限JSch。它工作得非常顺利。

于 2014-07-21T12:31:40.217 回答