Thursday, April 12, 2012

Cannot download a file using HttpServletResponse

I want to download a MS Word 2003 document that I am creating with the content myString in it.
I have used the following code:


BufferedReader reader = new BufferedReader(new FileReader(templatePathFilename));
 HttpServletResponse response = new MyHttpServletResponse();
 response.setContentType ("application/msword"); 
 response.setHeader ("Content-Disposition", "attachment; filename=\""+outgoingFileName); 
 ServletOutputStream myOut = response.getOutputStream();
 myOut.write(myString.getBytes());
 myOut.flush();
 myOut.close();
 


But the line myOut.write(myString.getBytes()) gives a NullPointerException.
MyHttpServletResponse is a class that eclipse has produced by default when I quick fix the error generated. Do I need to modify that class?
Can anyone please help!




If you are using a Java EE platform to perform the download operation to occur then the safest way is to do it through a servlet. A Sample Download servlet should Look like below,
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DownloadFile extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ PrintWriter out = response.getWriter();
String fileName = request.getParameter("fileName");
fileName=fileName.substring(fileName.lastIndexOf("\\")+1,fileName.length());
String extension=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
String filePath = "C:/temp/"+fileName;
FileInputStream fileToDownload = new FileInputStream(filePath);
//ServletOutputStream output = response.getOutputStream();
if(extension.equalsIgnoreCase("txt")){
    response.setContentType("text/html");
}
else if(extension.equalsIgnoreCase("doc")){
    response.setContentType("application/msword");
}
else if(extension.equalsIgnoreCase("pdf")){
    response.setContentType("application/pdf"); }
else if(extension.equalsIgnoreCase("jpg")){
    response.setContentType("image/jpeg"); }
else if(extension.equalsIgnoreCase("jpeg")){
    response.setContentType("image/jpeg");
}
response.setHeader("Content-Disposition","attachment; filename="+fileName);
response.setContentLength(fileToDownload.available());
int c;
while((c=fileToDownload.read()) != -1){
out.write(c);
}
out.flush();
out.close();
fileToDownload.close();
}
}

1 comment:

  1. As you mentioned MyHttpServletResponse is a class that eclipse has produced by default when I quick fix the error generated, that seems to be the problem.

    Your code should be inside some servlet/JSP and the HttpServletResponse object should be taken from the container (which is passed to service/doGet/doPost methods).

    What you are using is a default/dummy implementation of HttpServletResponse which gives response.getOutputStream(); as null. If you use the container provided objects, your problem will be solved.

    ReplyDelete