File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ftp from servlet to client

 
Susan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can someone please tell me how to ftp from a servlet to a client. I have tried Sun and Apache's ftp client classes, sockets and URLConnection. In all cases the ftp just results in the file being "ftp'd" to the root directory of my account on my web host. The snippet of code I am currently using with a URL Connection is:

BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
URL url = new URL("ftp://user wd@ftpServer/fileName;type=i"); // i.e. ftp location URL
URLConnection urlc = url.openConnection();

bis = new BufferedInputStream(urlc.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(destination.getName()));

int i;
while ((i = bis.read()) != -1) {
bos.write(i);
}

///////////////////////////////

Do I need an additional connection to the client? If so, can I get an example?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe if you describe, exactly, what you are trying to do, someone could help.
A servlet is not going to be able to initiate an FTP session with a client unless the client is running an FTP Server.
 
Susan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Maybe if you describe, exactly, what you are trying to do, someone could help.
A servlet is not going to be able to initiate an FTP session with a client unless the client is running an FTP Server.
 
Susan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a number of files on my anonymous ftp site on my web server. I want to programmatically download these to client machines without revealing the program locations.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like this....







Build the username/password into the ftp string like..
ftp://anonymous:someone_somewhere.com@ftpdomain.com/dir/filename..
[ March 20, 2005: Message edited by: Ben Souther ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Susan (moose) nospamthanks",
we're a friendly bunch here at the Ranch, but one rul we take quite seriously is the one about display names. You can read about it here.

Basically we require your display name to be two words: Your first name, a space, then your last name. Obviously fictitious names are not allowed.

Please edit your profile and select a valid display name. Acounts with invalid display names get deleted, often without warning

thanks,
Dave.
 
Susan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Try something like this....







Build the username/password into the ftp string like..
ftp://anonymous:someone_somewhere.com@ftpdomain.com/dir/filename..

[ March 20, 2005: Message edited by: Ben Souther ]



Thanks I'll give it a try.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Susan Nospamthanks",
sorry, this is still fictitious and not allowed. Please try again.

thanks,
Dave.
 
Susan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have attempted the code as indicated above, however, although it got me to the point of saving the file to the client, the downloaded file was only 1K.

I set the mime content to "application/octet-stream" to indicate the files were of type "bin" and/or "exe". I have tried both the buffered byte read and the byte-by-byte file reads and it makes no difference. I have checked the ulimit on the host and it says "unlimited". Any idea what might be the problem?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Susan,
Out of respect for the moderators, who are trying to maintain a professional environment here, many people (myself included) will refrain from continuing converstaions with someone until they have complied with any outstanding requests from the said moderators.

(fix your name: <first-name> <last-name> )

-Ben
[ March 21, 2005: Message edited by: Ben Souther ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic