*
The moose likes Servlets and the fly likes Download FTP file from remote server in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Download FTP file from remote server in servlet" Watch "Download FTP file from remote server in servlet" New topic
Author

Download FTP file from remote server in servlet

Ricardo Marques
Greenhorn

Joined: May 08, 2002
Posts: 27
Hello everybody.
I'm trying to build a servlet which will be hosted at server A, and needs to download (to a client C, via web browser) a file stored at server B.
Can this be done? What is the best way to do this? Can I have the full steps please?
What about the upload? Can it also be done? Upload a file in client C, through a servlet stored in A, to a server stored in B.
Thank you all.
[ May 20, 2002: Message edited by: Ricardo Marques ]
[ May 20, 2002: Message edited by: Ricardo Marques ]

Sun Certified Programmer for the Java 2 Platform 1.4<p>If I can create, I will make mistakes. If I can learn with them, it must be some kind of art.
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228
Hi,
I just did a part of that i.e. the part of downloading from a remote machine.I used the URLConnection class in the java.net package of the API. Its pretty simple actually. Heres the code fragment that does that
public static boolean getRemoteFile(String bucketName, String articleId, boolean print){
//Use the Constructor URL(String protocol, String host, int port,, String file) getting all the parameters from the properties file
String protocol = rb.getString(bucketName+"protocol");
String url = rb.getString(bucketName+"url");
int port = Integer.parseInt(rb.getString(bucketName+"port"));
String dir = rb.getString(bucketName+"dir");
int i=0;
String folder = articleId.substring(0,2)+"/"+articleId.substring(2,4);
urls= new URL[7];
boolean status = false;
try{
urls[0] = new URL(protocol, url, port, dir+folder+"/"+articleId+"/"+articleId+"_meta.cf");
urls[1] = new URL(protocol, url, port, dir+folder+"/"+articleId+"/"+articleId+"_print.cf");
urls[2]= new URL(protocol,url,port,dir+folder+"/"+articleId+"/"+articleId+"_1.cf");
urls[3]= new URL(protocol,url,port,dir+folder+"/"+articleId+"/"+articleId+"_2.cf");
urls[4]= new URL(protocol,url,port,dir+folder+"/"+articleId+"/"+articleId+"_3.cf");
urls[5]= new URL(protocol,url,port,dir+folder+"/"+articleId+"/"+articleId+"_4.cf");
urls[6]= new URL(protocol,url,port,dir+folder+"/"+articleId+"/"+articleId+"_5.cf");

//url = new URL("http","www.xyz.com",81,"/content2/weblogic/content/2002/00/"+folder+"/"+articleId+"/"+articleId+"_1.cf");
//url = new URL("http","<other m/cs Url e.g. www.xyz.com>",81,"/content2/weblogic/content/2002/00/"+folder+"/"+articleId);
for(i = 0; i< urls.length;i++){
uconn = urls[i].openConnection();
is = uconn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
while ((line = in.readLine()) != null){
completeString += line + "\n";
}
if(!completeString.equals("")){
String loc = rb.getString("Location");
if(print)
status = printRemoteFile(bucketName, loc+urls[i].getFile().substring(urls[i].getFile().indexOf(loc)+loc.length(), urls[i].getFile().length()), completeString);
else
status = true;
}
completeString = "";
}
}catch(MalformedURLException mue){
System.out.println("Exception Occured:"+mue.toString());
mue.printStackTrace();
return false;
}catch(FileNotFoundException fnfe){
if(i >2){
log("Meta, Print and at least _1.cf found");
return true;
}else{
log("Not all the necessary files found so not generating the link for article id:"+articleId);
//fnfe.printStackTrace();
return false;
}
}catch(Exception e){
System.out.println("Exception Occured:"+e.toString());
e.printStackTrace();
return false;
}finally{
urls = null;
uconn = null;
is = null;
}
return status;
}

PLEASE COPY THIS AWFUL LOOKING CODE INTO AN EDITOR AND IT WILL START MAKING SENSE
HTH
SAHIL
[ May 20, 2002: Message edited by: sandy ind ]

Adios
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Download FTP file from remote server in servlet
 
Similar Threads
J2ME Hot or not?
Regarding FTP froma remote server
Help Plz (Urgent)
Passed SCWCD Exam Today (May 09)
Uploading images to database from html/jsp admin page