Iam currently developing an application which generates and XML file in my current env(App Server).I need to upload this XML file to an remote location using FTP.Would anyone help me out in getting the working source code for file upload via FTP.
Iam tried writing code but iam getting
java.net.UnknownServiceException: protocol doesn't support outputHere
java.io.FileNotFoundException: \trmsdev\ftp\inbound\cPDM (The system cannot find the path specified)
My Source code
***************************************************************************
import java.net.*;
import java.io.*;
class ftpClient
{
URLConnection connection;
InputStream is;
OutputStream os;
String url;
String user;
String password;
public ftpClient(String url,String user,String password)
{
this.url =url;
this.user= user;
this.password=password;
}
private void getConnection(String file)
{
try
{
StringBuffer location = new StringBuffer();
location.append("ftp://");
location.append(user + ":");
location.append(password + "@");
location.append(url + "/");
location.append(file +";type=i");
connection = (new URL(location.toString())).openConnection();
//is = connection.getInputStream();
os = connection.getOutputStream();
System.out.println("The file location::"+location);
}
catch(Exception e)
{
System.out.println(e.toString() + "Here" );
}
}
public void download( String inputFile,String outputFile ) throws Exception
{
try
{
getConnection( inputFile );
int data;
InputStreamReader fileReader = new InputStreamReader(is);
FileOutputStream writer = new FileOutputStream( new File(outputFile) );
while ( (data = fileReader.read()) != -1 )
{
writer.write((char)data);
}
}
catch(Exception e)
{
System.out.println(e );
}
}
public void upload( String inputFile,String outputFile ) throws Exception
{
try
{
System.out.println("Coming inside the upload method...");
getConnection( outputFile );
BufferedInputStream in = new BufferedInputStream(new FileInputStream(inputFile));
FileOutputStream writer = new FileOutputStream( new File(outputFile) );
byte[] buffer = new byte[1024];
int numberRead;
while ((numberRead = in.read(buffer)) >= 0) {
writer.write(buffer, 0, numberRead);
}
System.out.println("Writing the file...");
}
catch(Exception e)
{
System.out.println(e );
}
}
public static void main(String[] args)
{
String outputFile = "/trmsdev/ftp/inbound/cPDM/";
String inputFile = "C:\\test.doc";
try
{
ftpClient client = new ftpClient("mbht002.server.com","trainr","password");
//client.download(inputFile,outputFile);
client.upload(inputFile,outputFile);
System.out.println( inputFile + " Uploaded successfully to " + outputFile);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
please examine the method upload().
Your help is gratly appreciated and welcomed.
With Rgds
[email protected]