using HTTP protocol in Java

Below is the code which will provide the process using which we can read a file through HTTP protocol & write the o/p through FTP on another location


Note: The

Here source can be any fle say .pdf file

E.g: http://serverip/filepath/fax.pdf;


URL url = new URL(source);

URLConnection urlC = url.openConnection();
InputStream is = url.openStream();

Date date = new Date(urlC.getLastModified());
System.out.flush();

//
//FTP conenction code

ResourceBundle myResources = new Resource_Properties().getBundle_Ftp();

String ftphost=myResources.getString("ftp_host");
String ftpuser=myResources.getString("ftp_username");
String ftppassword=myResources.getString("ftp_pwd");
int ftpport=Integer.parseInt(myResources.getString("ftp_port"));
int ftpportread=Integer.parseInt(myResources.getString("ftp_portread"));

FTPClient ftp = new FTPClient();
ftp.connect(ftphost,ftpport);
ftp.login(ftpuser,ftppassword);
System.out.println("successfully connected");

URL u = new URL("http://"+ftphost+":"+ftpportread+""+targetlocation);

HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
int rc = huc.getResponseCode();

if(rc==404)
{
//targetlocationloc is the target folder in which we need to write the FTP o/p

ftp.makeDirectory(targetlocationloc);

}//if


//targetfile is the o/p file

String pathh= targetlocation+targetfile;

FileOutputStream fos = null;
fos = new FileOutputStream((targetfile));
int oneChar;
while((oneChar = is.read()) != -1)
{
fos.write(oneChar);
count++;

}
is.close();
fos.close();

File fullFile = new File(targetfile);
FileInputStream fis=new FileInputStream(fullFile);
ftptest ft=new ftptest(fis,pathh );
fullFile.delete();

No comments:

Post a Comment