I would like to download a file from an sftp server by using the class Runtime of Java. I can't use a library like Jsch or SSHJ because i need to use the -B option to increase the buffer size. I've tried with this code so far:
public void download(String source, String destination,String port, String user, String host ){
Runtime runtime = Runtime.getRuntime();
String cmd = "sftp -oPort=" + port + " -B 150000 "+ user + "@" + host + ":" + source + " " + destination;
String[] args = { "/bin/sh", "-c", cmd };
try {
long startTime = System.currentTimeMillis();
log.info("cmd: "+ cmd);
final Process process = runtime.exec(args);
process.waitFor();
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println("Time: "+elapsedTime);
BufferedReader bre = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuffer sbe = new StringBuffer();
String lineError;
while ((lineError = bre.readLine()) != null) {
sbe.append(lineError).append("\n");
}
String answerError = sbe.toString();
if(answerError.length()>0)log.error("Error:"+answerError);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I get the error message : Connecting to xxxxxxx... with the name of the host instead of the "xxxxxx"
I've tried the command directly in putty and it works great but when i use it in java it doesn't work at all. I've also tried an scp command with runtime.exec() for downloading instead of an sftp and that work.
Does someone have a clue why it's not working? I can't find an exemple with this method. Or does someone know a library for sftp transfer that allows to set the -B option? i've maybe missed a method in Jsch and Sshj.
Thanks in advance