79638405

Date: 2025-05-26 06:28:38
Score: 0.5
Natty:
Report link

@MartinPrikryl regarding your comment, I tried with

private static final int PORT = 6323;
private static final String USERNAME = "tsbulk";
private static final String HOST = "<SERVER>";
private static final String ROOT_DIRECTORY = "/tsbulk-prod";
private static final String PRIVATE_KEY = "<PRIVATE-KEY-PATH>"

public static void main(String[] args) throws JSchException, IOException, SftpException {
    Path file = Files.write(createTempFile("foo", "bar"), "test".getBytes(UTF_8));
    JSch jSch = new JSch();
    jSch.addIdentity(PRIVATE_KEY);
    Session session = jSch.getSession(USERNAME, HOST, PORT);
    session.setConfig("StrictHostKeyChecking", "no");
    session.setConfig("PreferredAuthentications", "publickey");
    session.connect();
    ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
    channel.connect();
    channel.put(file.toString(), ROOT_DIRECTORY + "/" + file.getFileName());
    channel.rm(ROOT_DIRECTORY + "/" + file.getFileName());
    channel.disconnect();
    session.disconnect();
}

and it's working fine. The file gets uploaded and no errors are triggered. Of course, this is a bit a simplified setup given that it doesn't handle sub directories.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @MartinPrikryl
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: cdprete