File Download with Java


EJB programming & troubleshooting: File Download with Java

  1. File Download with Java (5 messages)

    I want to implement the facility of downloading files through my web application, which are stored on some directory (say c:\files) on the server. How can i do this using jsp,EJB?

    Threaded Messages (5)

  2. File Download with Java[ Go to top ]

    Do you mean downloading (from server to client) or upload (from client to server)?
  3. File Download with Java[ Go to top ]

    Re-reading you post I guess you meant from the server. I don't know if you can do this out of the web context but within the web context there are various techniques. One the works well is to use a servlet output stream – I'm passing the resource path info as extra path info on the HTTP – you could also do this as a query string or whatever.

    So: given a path like http://localhost:8080/twofish/servlet/ViewResource/mp3s/TimesTablas.mp3

    My view resource servlet:
    // Use a ServletOutputStream because we may pass binary information
      final ServletOutputStream out = res.getOutputStream();

         //Get the URL
        // Get the resource to view
        String file = req.getPathInfo();
        if (file == null) {
          out.println("Extra path info was null; should be a resource to view");

        // Convert the resource to a URL
        URL url = getServletContext().getResource(file);
        if (url == null) { // some servers return null if not found
          out.println("Resource " + file + " not found");

    //Serve the file
    InputStream in = url.openStream();
        byte[] buf = new byte[4 * 1024]; // 4K buffer
        int bytesRead;
        while ((bytesRead = != -1) {
          out.write(buf, 0, bytesRead);

    Pushes out the TimesTablas.mp3 from the mp3's directory.

    Most browsers, when they receive the “application/octet-stream“ content type, offer the user a dialog box asking them where to save the content. The exception to this rule is, of course, Microsoft’s Internet Explorer, which, if it recognises the content type delivered, ignores the server-assigned content and displays the content normally so you may need to prompt your users to right click and do "Save As"
  4. File Download with Java[ Go to top ]

    How can I implement this using with multiple threads.
    I mean I would like to down load a file from a webserver with multiple connections to the file and download it.

    Like download manager apps.
  5. First of all, thanks to Fred Bloggs who set me on the right path. The problem with storing your files in the web-context is that everyone with a browser can get at them by entering the right string in the address field of the browser. This solution is very close to that of Fred Bloggs.

    First, it's done in the doGet of a servlet. Send the filepath info like so:
    Click <a href="DownLoadServlet?file=C:\\uploads
    here</a> to download FSG.doc

    The following is the same as in Fred's posting, just for completeness sake:
    final ServletOutputStream out = res.getOutputStream(); res.setContentType("application/octet-stream");
    Some differences in the following:
    File file = new File(req.getParameter("file"));
    BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
    byte[] buf = new byte[4 * 1024]; // 4K buffer
    int bytesRead;
    while ((bytesRead = != -1) {
    out.write(buf, 0, bytesRead);

    And that's it. The only change I'm going to make is to open this .doc file in OpenOffice and export it as a pdf.
  6. File Download with Java[ Go to top ]

    Thanks that worked!