有没有办法通过Java中的VFS设置远程文件夹的权限?

时间:2014-07-18 13:32:18

标签: java sftp apache-commons-vfs

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

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

我上传XML和ZIP时创建了文件夹<hash>,我需要此文件夹才能拥有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权限的文件夹?

1 个答案:

答案 0 :(得分: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>目录。如果目录尚未存在,VFS将创建目录<hash>。上传文件后,我使用<hash>更改目录JSch的文件权限。它的工作非常顺利。

相关问题