有没有一种方法可以通过Java中的VFS设置远程文件夹的权限?

基督教

我创建一个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

基督教

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

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它的工作非常顺利。

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有一种方法可以轻松设置文件夹使用的图标?

有没有一种方法可以在Spring外部公开要通过Internet访问的文件夹?

有没有一种方法可以'tail -f'文件夹?

有没有一种方法可以读取C ++中的文件文件夹?

有没有一种方法可以在PhpStorm中搜索文件夹(目录)?

有没有一种快速的方法可以在文件夹结构中查找(特定用户的)权限?

有没有一种方法可以为资源管理器中的文件夹设置不同的颜色?

有没有一种简单的方法可以在谷歌驱动器中批量设置子文件夹权限?

有没有一种方法可以设置默认应用程序以打开特定文件夹的所有文件类型?

有没有一种方法可以强制Maven增量复制资源文件夹更改?

有没有一种方法可以禁用Windows自动选择文件夹模板?

有没有一种方法可以更改Visual Studio Code的扩展文件夹位置?

在Windows上,有没有一种方法可以将选定的文本作为文件夹打开?

有没有一种方法可以自动要求PR作者从分支/分支删除文件夹?

有没有一种方法可以在../sdk/tools文件夹之外执行android命令?

有没有一种方法可以检查Dropbox中的文件/文件夹是否具有共享链接而不创建链接?

有没有一种方法可以轻松共享Visual Studio Online / Team Foundation Server的工作区设置(文件夹映射)?

有没有一种方法可以写入同一文件夹中的多个文件?

有没有一种方法可以使用ClassLoader和getClass()方法从res文件夹中获取文件

有没有一种方法可以将存档的logback的.gz日志文件存储在单独的文件夹中?

有没有一种方法可以使用用户输入批量重命名文件夹中的文件?

有没有一种方法可以加载不同于java_home / jre / lib / security文件夹中指定的证书的证书?

有没有一种方法可以防止安装/更新使我的硬盘驱动器中的文件夹隐匿?

在MacOS X中,有没有一种方法可以为只读驱动器上的文件夹创建快捷方式?

有没有一种方法可以在构建中包括目录,而不必将其放在资产文件夹中?

有没有一种方法可以快速更改文件夹中所有文件的所有文件扩展名?

有没有一种方法可以通过httpurlconnection下载.jar文件?

有没有一种方法可以通过键盘在Xcode9中设置查找选项?

有没有一种方法可以从通过循环创建的表中设置$ _SESSION变量