java 使用 JSch 拒绝权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33243278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:25:13  来源:igfitidea点击:

Permission denied using JSch

javasftpjsch

提问by Iván Travecedo

I'm trying to retrieve some files from sftp server using JSch but I'm getting the following error.

我正在尝试使用 JSch 从 sftp 服务器检索一些文件,但出现以下错误。

3: Permission denied
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
at com.company.common.sftp.impl.managedFile.moveFiles(managedFile.java:712)

Here is the code:

这是代码:

private List<String> moveFiles(String prefixFileName, String path) {
    Session session = getSession();
    Channel channel = connect(session);
    ChannelSftp channelSftp = null;
    try {
        channelSftp = (ChannelSftp)channel;
        channelSftp.cd(_workingDir);
    ...
    }
    ...
    finally {
        channel.disconnect();
        session.disconnect();
    }
}

public Session getSession() {              
    Session session = null;
    JSch jsch = new JSch();
    session = jsch.getSession(_user,_server,_port);
    session.setPassword(_password);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", _strictHostKeyChecking);
    session.setConfig(config);
    session.connect();
    return session;
 }

public static Channel connect(Session session) {
  Channel channel = null;
  channel = session.openChannel("sftp");
  channel.connect();
  return channel;
}

_workingDiris a property with the following value: /user_files. Both folders (source and destination) are on a Windows server and all the privileges was granted to any user. But for some reason it doesn't let me change the current directory in the source (remote) server.

_workingDir是具有以下值的属性:/user_files. 两个文件夹(源和目标)都在 Windows 服务器上,所有权限都授予任何用户。但出于某种原因,它不允许我更改源(远程)服务器中的当前目录。

Any idea?

任何的想法?

UPDATE:The Sftp server is freeFTPd and using a sftp client (like Filezilla) I can move files without problems

更新:Sftp 服务器是 freeFTPd 并且使用 sftp 客户端(如 Filezilla)我可以毫无问题地移动文件

采纳答案by PeterMmm

Probably /user_filesis an absolute path.

大概/user_files是绝对路径。

Try ./user_filesfor a relative path to the user's home directory.

尝试./user_files获取用户主目录的相对路径。

In Filezilla, it is C:\user_fileson the remote side ?

在 Filezilla 中,它C:\user_files在远程端?

回答by Bilal Ahmed Yaseen

The same issue was encountered by me and verifying the followings fixed my issue:

我遇到了同样的问题,并验证以下解决了我的问题:

  1. Path of the file we are trying to retrieve doesn't exist. So, please make sure that file actually exists under the directory that your application is accessing to retrieve the file.
  2. Make sure SFTP user that you are using in the application to make the connection is configured and active in SFTP.
  3. If above both points are valid in your scenario then try after rebooting the SFTP service.
  1. 我们尝试检索的文件的路径不存在。因此,请确保该文件确实存在于您的应用程序正在访问以检索文件的目录下。
  2. 确保您在应用程序中用于建立连接的 SFTP 用户已在 SFTP 中配置并处于活动状态。
  3. 如果以上两点在您的场景中都有效,请在重新启动 SFTP 服务后尝试。

回答by u11872415

I have also meet the problem when using jsch in docker container, and fix the issue after chowning with the folder and starting sftp with root :

我在 docker 容器中使用 jsch 时也遇到了这个问题,在 chowning 文件夹并用 root 启动 sftp 后解决了这个问题:

  1. Start up the application with root ,not the other user, because it will work with the dir "/var/run" owned by root;
  2. Because of (1), you should have a path owned by root to be the data path, you'd better give full privileges to the path:chmod 777 /yourpath;
  3. Mention: you should mkdir when logining the sftp server with "sftp -P 22 [email protected]", not to do it in data path, because the path will owned by root or other system user, but not the user in your sftp system, so that you can not upload files(because permission denied);
  4. After changing all of above, I can finally upload and download files on the server.
  1. 使用 root 启动应用程序,而不是其他用户,因为它将使用 root 拥有的目录“/var/run”;
  2. 由于(1),你应该有一个root拥有的路径作为数据路径,你最好给路径完全权限:chmod 777 /yourpath;
  3. 提示:使用“sftp -P 22 [email protected]”登录sftp服务器时应该使用mkdir,不要在数据路径中进行,因为该路径将由root或其他系统用户拥有,而不是sftp中的用户系统,使您无法上传文件(因为权限被拒绝);
  4. 完成以上所有更改后,我终于可以在服务器上上传和下载文件了。