package com.zehon.ftp.samples;
/**
* This UploadFileAsStreamSample
class
* This sample demonstrates how to upload a file (represented as a java.io.InputStream) via FTP
* This sample uses a convenient method, for a Spring-able version of this refer to FTPClient
* @see com.zehon.ftp.FTPClient
* Please refer to http://www.zehon.com/features_ftp.htm for more information about our FTP.
* @author Zehon Team (we're happy to serve you!) http://www.zehon.com/
*
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import com.zehon.FileTransferStatus;
import com.zehon.exception.FileTransferException;
import com.zehon.ftp.FTP;
public class UploadFileAsStreamSample {
/**
* Please refer to http://www.zehon.com/FTP_samples.htm for its full source code
* @param args
*/
public static void main(String[] args) {
String host = "ftp.zehon.com";
String username = "ftp";
String password = "ftp";
String filePath = "C:\\myfiles\\files\\test.txt"; //loading this file
InputStream is = null;
File localFile = new File(filePath);
try {
is = new BufferedInputStream(new FileInputStream(localFile));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
String destFolder = "/test";
String nameOfFile = "testStream.txt";
try {
int status = FTP.sendFile(is, nameOfFile, destFolder, host, username, password);
if(FileTransferStatus.SUCCESS == status){
System.out.println(filePath + " got ftp-ed successfully to folder "+destFolder);
}
else if(FileTransferStatus.FAILURE == status){
System.out.println("Fail to ftp to folder "+destFolder);
}
} catch (FileTransferException e) {
e.printStackTrace();
}
}
}