Friday, July 24, 2009

How to upload a file in to the ftp server through java

package com.evolvus.documentmanagement.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPReply;

/***
* This is an program demonstrating how to use the FTPClient class.
* This program connects to an FTP server and retrieves the specified
* file.
***/
public final class FTPUtil
{
public boolean insertIntoFTP(String server,String port,String username,String password,String remote,String local,String directoryName,byte[] data){
boolean error = false;
FTPClient ftp = null;
try
{
ftp = new FTPClient();
this.getFTPConnection(ftp, server,port, username, password,directoryName);
if(ftp!=null){
InputStream input;
input = new ByteArrayInputStream(data);
ftp.storeFile(remote, input);
}
}catch (FTPConnectionClosedException e)
{
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
}
catch (IOException e)
{
error = true;
e.printStackTrace();
}
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
}
return error;
}
public byte[] getDataFromFTP(String server,String port,String username,String password,String remote,String directoryName,String local){
byte[] data = null ;
boolean error = false;
FTPClient ftp = null;
try
{
ftp = new FTPClient();
this.getFTPConnection(ftp, server,port, username, password,directoryName);
if(ftp!=null){
ByteArrayOutputStream output;

output = new ByteArrayOutputStream();
ftp.retrieveFile(remote, output);
data=output.toByteArray() ;
output.close();
}
}
catch (FTPConnectionClosedException e)
{
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
}
catch (IOException e)
{
error = true;
e.printStackTrace();
}
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
return data;
}
}

public FTPClient getFTPConnection(FTPClient ftp,String server,String port,String username,String password,String directoryName){
boolean error = false;
try
{
int reply;
if(port!=null &&port.trim().length()>0)
ftp.connect(server,Integer.parseInt(port));
else
ftp.connect(server);
System.out.println("Connected to " + server + ".");

// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();

if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
System.err.println("FTP server refused connection.");
}
if (!ftp.login(username, password))
{
ftp.logout();
error = true;
}
if(!ftp.changeWorkingDirectory(directoryName)){
ftp.mkd(directoryName);
}
ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
catch (IOException e)
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException f)
{
// do nothing
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
}
return ftp;
}
public static void main(String[] args) {
FTPUtil util = new FTPUtil();
util.insertIntoFTP("192.168.1.1", "65300", "user1", "ftp*evolvus", "test123", "test", "test123", "test".getBytes());
}
}

0 comments:

Post a Comment