mwiede / jsch

fork of the popular jsch library
Other
662 stars 124 forks source link

ChannelSftp.put() throws SftpException on some Android devices #538

Closed IgeNiaI closed 1 month ago

IgeNiaI commented 2 months ago

I had this issue with original jsch and still have it with version 0.2.12. I don't know if it was fixed in later releases cause using them requires updating gradle in my project, and that is problematic. Anyway, I get this stacktrace when trying to upload a file to sftp drive

[DefaultDispatcher-worker-6] Error uploading debug report to SFTP server: com.jcraft.jsch.SftpException: 
    at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2816)
    at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:544)
    at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:436)
    at com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:332)

And here's my upload function, in Kotlin:

    private fun uploadToFtp(srcFile: String, dstFile: String): Boolean {
        val session = JSch().getSession(USERNAME, HOST, PORT).apply {
            setPassword(PASSWORD)
            setConfig(Properties().apply {
                setProperty("StrictHostKeyChecking", "no")
            })
        }
        var channelSftp: ChannelSftp? = null

        return try {
            session.connect()

            channelSftp = session.openChannel("sftp") as ChannelSftp
            channelSftp.connect()
            channelSftp.put(srcFile, SFTP_SERVER_HOME_DIR + dstFile)

            true
        } catch (e: JSchException) {
            logger.error("Error connecting to SFTP server: ", e)
            false
        } catch (e: SftpException) {
            logger.error("Error uploading debug report to SFTP server: ", e)
            false
        } finally {
            channelSftp?.disconnect()
            session.disconnect()
        }
    }

Looking at jsch source code, this is thrown when header type is equal to SSH_FXP_STATUS, but that's not really explanatory

IgeNiaI commented 1 month ago

Nevermind, it was because my file name contained invalid characters. Still, the error is not explanatory at all.