File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Directory Download Servlet(Need your help asap!) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Directory Download Servlet(Need your help asap!)" Watch "Directory Download Servlet(Need your help asap!)" New topic

Directory Download Servlet(Need your help asap!)

Sumit Israni

Joined: Jun 20, 2001
Posts: 28
Hey Folks,
Could you please help me sort out this problem urgently.
Well I am falling short of the required time to do it myself!
Hence it is a request.I will be much grateful to anyone who can just help me out .
I am wanting to download an entire directory of images
and want to do so using the com.oreilly.servlet package.
I am also using the ServletUtils class to get it done.
My code is divided into 2 parts
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.*;
public class DownloadFile extends HttpServlet{
HttpServletResponse res;
ServletOutputStream out;
boolean endedLastResponse = true;

public void init(HttpServletResponse response) throws ServletException,IOException {
// Save the response object and output stream
res = response;
out = res.getOutputStream();
// Set things up

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
ServletOutputStream out = resp.getOutputStream();

ServletUtilsa.returnFile(req.getRealPath("/Dir/"), out);

MultipartResponse multi = new MultipartResponse(res);
out.println("On your mark");
try { Thread.sleep(1000); } catch (InterruptedException e) { }
out.println("Get set");
*********End of**************

import java.lang.reflect.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletUtilsa {
public static void returnFile(String filename,OutputStream out)
throws FileNotFoundException, IOException {
// A FileInputStream is for bytes
FileInputStream fis = null;
File f1=new File(filename);
String s[]=f1.list();

int i;
for (i=0;i<s.length;i++)
byte[] buf=new byte[4 * 1024];

try {
fis = new FileInputStream(filename);
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = != -1) {
out.write(buf, 0, bytesRead);
finally {
if (fis != null) fis.close();

**********End of*************
Both the files get compiled but I get a NullPointer Exception at output .I want to rectify it and want the popup dialog Box to be displayed for the user to download the directory of Images.
As you can see the directory is specified in this statement.
ServletUtilsa.returnFile(req.getRealPath("/Dir/"), out);
I need a workable solution have the source code as is!Many many thanks.
P.S: Please post the entire code either at my email address:
or here whichever is convenient

I agree. Here's the link:
subject: Directory Download Servlet(Need your help asap!)
It's not a secret anymore!