Search code examples
javasftpjsch

Permission denied using JSch


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

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;
}

_workingDir is 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.

Any idea?

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


Solution

  • Probably /user_files is an absolute path.

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

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