0

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 :

  • I set the url-mapping as /filedl, but I can download files from / . I would like to allow dl in /filedl only
  • Whatever file I download, it is named filedl.
  • I don't have access to the file list (Error 500), even though I have
  <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 !

jlengrand
  • 12,152
  • 14
  • 57
  • 87

1 Answers1

3

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.

npe
  • 15,395
  • 1
  • 56
  • 55
  • Thx for the info, my concern with Jetty is mainly that I get 404 all the time. there is no way I can find to actually get the root folder of my web app. The fact that I am a web dev newbie surely doesn't help :s – jlengrand Jun 22 '12 at 12:44
  • Try executing this in your servlet: `new File(".").getAbsolutePath()` should show you where in the path you are whe running the servlet. – npe Jun 22 '12 at 12:49
  • Hey, I can download files thanks :). I still have an issue though. See my edit – jlengrand Jun 22 '12 at 13:20
  • Not sure what your asking for... What other issue? – npe Jun 22 '12 at 13:24
  • Still not sure, but updated the answer with a way of restricting access to the filesystem. Learn how [`File(String, String)`](http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#File%28java.lang.String,%20java.lang.String%29) works. – npe Jun 22 '12 at 13:31