jlengrand
jlengrand

Reputation: 12827

Jetty : Client file download

I have a java application running Jetty, and containing several servlets and some beans. The whole thing is handled using a .war file.

The architecture is already pretty big, and I don't handle all its concepts yet, so please excuse my ignorance.

What I would like to do is extremely simple. I created a new servlet pointing to a url : filedl.

I would simply want to store a file in there, that the client can download For example for the url : myapp/filedl/my_file.txt

I only need some kind of directory listing, and allow access only to this directory.

Later, I'll think about adding authentification, but this is another problem.

The main issue here is that I get completely lost in all the concepts of Jetty, and googling jety download file returns loads of dumb results.

Any directions would be greatly appreciated !

Thx.

EDIT:

Thx to #npe, I am now able to donwload files. There is still a strange thing left though :

  <init-param>
     <param-name>dirAllowed</param-name>
     <param-value>true</param-value>
  </init-param>

in my war file

Any idea where this is coming from ?

Thx again !

Upvotes: 0

Views: 7007

Answers (1)

npe
npe

Reputation: 15719

It has nothing to do with Jetty.

What you are trying to do is to create a Servlet that is serving static files.

Now, to do that, you need to implement something like this:

@WebServlet(name="myAwesomeServlet", urlPatterns={"/filedl"})
public class MyAwesomeServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String fileName = (String) request.getParameter("file");
        FileInputStream fis = null;

        try {

            fis = new FileInputStream(fileName);
            response.setContentType("application/octet-stream"); 

            OutputStream out = response.getOutputStream();
            IOUtils.copy(fis, out); // this is using apache-commons, 
                                    // make sure you provide required JARs

        } finally {            

            IOUtils.closeQuietly(out);  // this is using apache-commons, 
            IOUtils.closeQuietly(fis);  // make sure you provide required JARs

        }

    }
}

If you cannot use the Servlet 3.0 API, get rid of the @WebServlet annotation, and map the servlet manually in the web.xml.

Then just invoke the URL like this:

http://your.awesome.server.com/filedl?file=path/to/file 

If you want the URLs to be more RESTful, like this:

http://your.awesome.server.com/filedl/file/path/to/file 

you need to make some changes to the way the parameters are parsed. I'll leave it as an exercise to you. This case might be also a good one to implement it as a REST service (see: Building RESTful Web Services with JAX-RS - The Java EE 6 Tutorial).

Edit

If you want to somehow restrict the files being downloaded to a single folder, you need to implement this yourself. For example, you can force all files, to be looked-up inside a specific folder like this:

String fileName = (String) request.getParameter("file");
File realFileName = new File("/my/restricted/folder", fileName);

...
fis = new FileInputStream(realFileName);

This way, URLs like /fieldl/someFile.txt fill cause serving file from /my/restricted/folder/someFile.txt.

Upvotes: 3

Related Questions