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

Building an FTP Servlet

Tobias Ericsson
Greenhorn

Joined: Apr 27, 2009
Posts: 6
Hey guys

I just read this wikipedia article about "FTP servlets" which can wrap FTP traffic to HTTP(S) and I just decided to try and create one.

Now I thought my version of it would function like this: It receives a file request from another webapp which in turn got it from a user. It opens a FTP transfer from a local FTP server and 'wraps' this into HTTPS and returns this to the first webapp for the user to download.

It was just a sudden idea but it sort of appeals to me . I'm just at a loss as to where to start, both how to open this FTP connection and how to "wrap" it into HTTPS. I mean, is there any existing libraries that can do these things or..? Any suggestions?

//yzfr1
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I think you're reading more into that description than is actually there.

All your servlet would do would be this:

  • Take the file name from the HTTP request
  • Establish a connection to the FTP server
  • Send a GET request to the FTP server for that file
  • Stream the response from the FTP server to the servlet's output stream


  • Which is exactly what you started to describe. I think you just got carried away with the word "wrap" and thought it was some kind of advanced technique.
    Tobias Ericsson
    Greenhorn

    Joined: Apr 27, 2009
    Posts: 6
    Wow, yes, I think you're absolutely right .

    Thanks for clearing that up, seems a much simpler task all of a sudden!

    For the connection and GET request to the FTP server, will I need a special library, because thats not included in the standard JavaEE API, correct? Some googling turned up org.apache.commons.net.ftp, would that be a good option you think?

    Oh, and also, how do you mean I should "stream the response" to the output stream? Hmm, maybe that part will solve itself once I get a look at the API for this.

    Thanks!
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 41906
        
      63
    Apache Commons Net is a very good library (make sure to check out what else it has to offer, so it's in the back of your brain next time you need to address a networking problem), and its FTP client is rock solid - it's a good choice.


    Ping & DNS - my free Android networking tools app
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18570
        
        8

    Tobias Ericsson wrote:Oh, and also, how do you mean I should "stream the response" to the output stream? Hmm, maybe that part will solve itself once I get a look at the API for this.


    You're going to get an InputStream from the FTP object. Just copy all of the bytes from that to the OutputStream which is your servlet's response.
    Tobias Ericsson
    Greenhorn

    Joined: Apr 27, 2009
    Posts: 6
    Allright, cheers guys, appreciate it
    Will post back if I encounter any problems
    Tobias Ericsson
    Greenhorn

    Joined: Apr 27, 2009
    Posts: 6
    Hi again.

    I got a bit sidetracked, I got it into my head to try and make this app as "secure" as possible, so I thought I'd venture into letting tomcat handling the authentications using the FORM method in the web.xml. I did lots of googling and then created this "JDBCRealm" in the server.xml to make it connect to my mysql database to authenticate users, though it doesn't seem to work..

    Heres the additions to server.xml

    And to web.xml

    And nothing happens basically, the login.jsp is never shown when accessing the resources. I think the problem is that I don't have a auth-constraint defining roles, but since its supposed to connect to the DB to get the roles I'm not sure what to put there...

    I'm sorry, I know this is really OT and I should probably create a new thread about it, I just thought that if its just something small that I've missed then you could just point it out? thanks
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Building an FTP Servlet