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
File1
ownloadFile.java
File2:ServletUtilsa.java
<code>
***********DownloadFile.java******************
import java.io.*;
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
res.setContentType("application/x-filler");
out.println();
out.println("--End");
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
{
ServletOutputStream out = resp.getOutputStream();
resp.setContentType("application/x-filler");
ServletUtilsa.returnFile(req.getRealPath("/Dir/"), out);
{
MultipartResponse multi = new MultipartResponse(res);
multi.startResponse("text/plain");
out.println("On your mark");
multi.endResponse();
try { Thread.sleep(1000); } catch (InterruptedException e) { }
multi.startResponse("text/plain");
out.println("Get set");
multi.endResponse();
multi.finish();
}
}
}
*********End of DownloadFile.java**************
</code>
<code>
*********ServletUtilsa.java*******************
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
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];
buf=s[i].getBytes();
out.write(buf);
}
try {
fis = new FileInputStream(filename);
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = fis.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
}
finally {
if (fis != null) fis.close();
}
}
}
**********End of ServletUtilsa.java*************
</code>
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 ..guys..you have the source code as is!Many many thanks.
P.S: Please post the entire code either at my email address:
sumiti@hotmail.com or here whichever is convenient