Monday, 17 August 2015

java - How do you upload a file to an FTP server?

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

public void onUpload()
String hostName = "xxxxxxx";
String username = "xxxxxxx";
String password = "xxxxxxx";
String ftpfolder = "outbound";

String fileName ="YourLocalFileName";
String remoteFilePath = "/" + ftpfolder + "/" + fileName;
String localFilePath = "YourLocalFilePath";
File file = new File(localFilePath);
if (!file.exists())
throw new RuntimeException("Error. Local file not found");

StandardFileSystemManager manager = new StandardFileSystemManager();
try {

// Create local file object
FileObject localFile = manager.resolveFile(file.getAbsolutePath());

// Create SFTP options
FileSystemOptions opts = new FileSystemOptions();

// SSH Key checking
opts, "no");
// Root directory set to user home

// Timeout is count by Milliseconds
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

// Create remote file object
FileObject remoteFile = manager.resolveFile(
createConnectionString(hostName, username, password,
remoteFilePath), opts);

// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
} catch (Exception e) {
//Catch and Show the exception
} finally {

public static String createConnectionString(String hostName,
String username, String password, String remoteFilePath) {
return "sftp://" + username + ":" + password + "@" + hostName + "/"
+ remoteFilePath;

Maven artifacts for Apache VFS is a Virtual File System library.



  1. Your blog is very informative, thanks for sharing it.  E-Health Care Lists is one of the global suppliers of healthcare mailing list & email list. Marketing to the healthcare industry, reaching the doctors and other healthcare decision makers is often an impossible task. Healthcare Mailing List


    1. genuine antivirus for 1 year , windows 7 pro sp1 keys and download , microsoft office 2007 professional key for sale , windows vista anytime upgrade free keygen , windows 7 kei , outlook 2010 product code , buy server 2013 r2 keys , oem key windows 7 , lRU7sh

      windows 7 ult key sale online

      buy office pro plus 2016 keys

      cheap windows 10 pro keys for sale

      windows server 2016 standard key sale and download

      buy windows 7 ult keys online

  2. This coding is really worked and so nice thanks admin for sharing this information.

    Render Cleaning Company
    K Rend Services

  3. Wow! I’m from Malaysia, an English teacher gonna be. Now, I know that my goal readers are my students in the past. custom essay writing service I’d like to mark like this too attractive event and real hard work to make an enormous article. Thank you for the suggestions, I really enjoyed reading this. It is so much inspiring.