This post shows how to run the native Unix sftp command in Java to upload files. We basically want to run the following command in Java. The Unix command is not directly translational to Java. However, we want to execute the same command. To accomplish this, we need to use Java’s
Runtime class.
1 | sftp -b batch_file.txt userid@remote-ftp-host:/home/userid/tmp/ |
Requirements
These are the things we used for this post.
- Windows 7 Professional SP1
- Eclipse – Kepler Release
- Java 1.7 (1.7.0_67 – Windows x86)
- Unix or Linux OS with sftp installed
The Codes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | /* * Copyright (C) 2014 www.turreta.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.turreta.ftp.unix; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.UUID; import com.turreta.thread.StreamGobbler; public class UnixSFTP { private static boolean doSFTP(String localFile, String ftpHost, String ftpUserId, String ftpDestPath) { // Build the command StringBuilder ftpCommand = new StringBuilder("sftp -b").append(" "); ftpCommand.append(ftpUserId).append("@").append(ftpHost).append(":"); String destPath = ftpDestPath; if (destPath != null && !destPath.trim().equalsIgnoreCase("")) { if (destPath.equals(".") || destPath.equals("./")) { destPath = "/."; } else if(!destPath.endsWith("/")) { destPath = destPath + "/"; } } else { return false; } // add the destination path to the command ftpCommand.append(destPath); boolean successful = false; UUID idOne = UUID.randomUUID(); String uuidfileName = idOne.toString(); File f = new File(uuidfileName); if (f.exists()) { f.delete(); } try { f.createNewFile(); FileWriter fw = new FileWriter(f); fw.write("put " + localFile); fw.close(); } catch (IOException e1) { return successful; } try { Process p = Runtime.getRuntime().exec(ftpCommand.toString()); com.turreta.thread.StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR", null); StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT", null); errorGobbler.start(); outputGobbler.start(); int exitVal = p.waitFor(); if(exitVal == 0) { successful = true; } } catch (Exception e) { successful = false; } return successful; } public static void main(String[] args) { // Sample usage UnixSFTP.doSFTP("/home/user123/testfile001.txt", "remote-ftp-host", "remoteuser01", "/"); } } |
Get the Codes