Below is the code which will provide the following:
Below is the FTP connection code. (Pl refer to my other post for the implementation of properties file to store & retrieving FTP credentials)
Below is the code which will check whether the target folder exists or not. If not exists will create a new folder. Then it will write the file to FTP
- Read a file through HTTP protocol
- Write the output to FTP on another location
- Implementation of FileOutputStream & FileInputStream
- Ready to use code
String Source="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();
Below is the FTP connection code. (Pl refer to my other post for the implementation of properties file to store & retrieving FTP credentials)
//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");
Below is the code which will check whether the target folder exists or not. If not exists will create a new folder. Then it will write the file to FTP
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