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>

14 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
  4. High technology WebRTC allowing high quality communications online between doctors and patients. healthcare mobile apps

    ReplyDelete
  5. Nowadays some healthcare providers have begun to rely heavily on technology. healthcare mobile apps allows patients email and video chat with physicians and schedule appointments. They are not only convenient for patients but also for physicians.

    ReplyDelete
  6. At Savvycom – A software development company, we have been offering strategic IT solutions to our clients since 2009, specifically, Savvycom’s domain technology is in healthcare mobile apps that build adherence and engagement. Our healthcare mobile app developers with their seasoned experience and passion for IT offer development services that are always in alignment with the clients’ vision as well as bring the greatest experience to both physicians and patients.

    ReplyDelete
  7. With mobile devices becoming more widely used, mobility has been increasingly adopted in the healthcare industry to improve, design and transform healthcare. From making connecting physicians and patients easier through technology to affording busy nurses precious extra time, mobile apps have brought benefits to stakeholders. Hospitals and patients alike gain benefits from the power of text, voice, and real time critical data access that healthcare mobile app developers provide through modern mobility. Mobility is expected to address issues healthcare facing today such profitability pressures or patient satisfaction.

    ReplyDelete
  8. Microsoft office has a different version, and the entire version has the different setup file. Some of the version is Office 2013, Office 2016 and Office 365 etc. Go to www.office.com/setup and redeem your key. Office Com Setup is one of the most popular productivity programs around. If you have your product key handy, you don't even need a disc to start installing Office just visit office setup help. Norton is one of the most reliable antivirus providers in the market to download visit www.norton.com/setup . There are multiple manufacturers making printers for home and office use with varied range as printer series per the need and affordability of different customers. Printer Tech Support. It has become one of the most useful computer hardware devices that allow people to convert computer generated typescript into a physical printed document.

    ReplyDelete
  9. contact for seo, smo, internet marketing and consulting....seo Company in noida sector 10

    ReplyDelete