views:

150

answers:

1

I have written a Java web application that allows a user to download files from a server. These files are quite large and so are zipped together before download.

It works like this:
1. The user gets a list of files that match his/her criteria
2. If the user likes a file and wants to download he/she selects it by checking a checkbox
3. The user then clicks "download"
4. The files are then zipped and stored on a servera
5. The user this then presented with a page which contains a link to the downloadable zip filea
6. However on downloading the zip file the file that is downloaded is 0 bytes in sizea

I have checked the remote server and the zip file is being created properly, all that is left is to serve the file the user somehow, can you see where I might be going wrong, or suggest a better way to serve the zip file.

The code that creates the link is:

<%   
String zipFileURL = (String) request.getAttribute("zipFileURL"); %>  
<p><a href="<% out.print(zipFileURL); %> ">Zip File Link</a></p>

The code that creates the zipFileURL variable is:

public static String zipFiles(ArrayList<String> fileList, String contextRootPath) {
        //time-stamping
          Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
    Iterator fileListIterator = fileList.iterator();
    String zipFileURL = "";

    try {
        String ZIP_LOC = contextRootPath + "WEB-INF" + SEP + "TempZipFiles" + SEP;
        BufferedInputStream origin = null;
        zipFileURL = ZIP_LOC
        + "FITS." + timeStamp.toString().replaceAll(":", ".").replaceAll(" ", ".") + ".zip";
        FileOutputStream dest = new FileOutputStream(ZIP_LOC
              + "FITS." + timeStamp.toString().replaceAll(":", ".").replaceAll(" ", ".") + ".zip");
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
              dest));

        // out.setMethod(ZipOutputStream.DEFLATED);
        byte data[] = new byte[BUFFER];

        while(fileListIterator.hasNext()) { 

           String fileName = (String) fileListIterator.next();
           System.out.println("Adding: " + fileName);
           FileInputStream fi = new FileInputStream(fileName);
           origin = new BufferedInputStream(fi, BUFFER);
           ZipEntry entry = new ZipEntry(fileName);
           out.putNextEntry(entry);
           int count;
           while ((count = origin.read(data, 0, BUFFER)) != -1) {
              out.write(data, 0, count);
           }
           origin.close();            
       }

      out.close();
   } catch (Exception e) {
      e.printStackTrace();
   }

return zipFileURL;
    }
+1  A: 

A URL cannot access any files (directly) under WEB-INF. I'd suggest using a servlet to return the file from whatever location it was saved to

Would also suggest saving the file outside the context of your webapp

objects
would appreciate a code snippet of how I would use a servlet to return a file. Thanks
Yaw Reuben
theres an example here:http://helpdesk.objects.com.au/java/how-can-i-return-an-image-using-a-servlet
objects