Monday, 17 August 2015

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

import java.io.File;
import java.io.FileNotFoundException;
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 {
manager.init();

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

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

// SSH Key checking
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
// Root directory set to user home
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts,
false);

// 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);
System.out.println("Done");
} catch (Exception e) {
//Catch and Show the exception
} finally {
manager.close();
win.detach();
}
}


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.


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.0</version>
</dependency>

5 comments:

  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

    ReplyDelete
    Replies

    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

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

    Render Cleaning Company
    K Rend Services

    ReplyDelete
  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.

    ReplyDelete