aspose file tools*
The moose likes Servlets and the fly likes Ftp from servlet to client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Ftp from servlet to client" Watch "Ftp from servlet to client" New topic
Author

Ftp from servlet to client

Susan
Greenhorn

Joined: Mar 18, 2005
Posts: 5
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Susan
Greenhorn

Joined: Mar 18, 2005
Posts: 5
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

Joined: Mar 18, 2005
Posts: 5
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Mar 06, 2001
Posts: 13459

"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

Joined: Mar 18, 2005
Posts: 5
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

Joined: Mar 06, 2001
Posts: 13459

"Susan Nospamthanks",
sorry, this is still fictitious and not allowed. Please try again.

thanks,
Dave.
Susan
Greenhorn

Joined: Mar 18, 2005
Posts: 5
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

Joined: Dec 11, 2004
Posts: 13410

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
 
subject: Ftp from servlet to client