Below is the Servlet code please let me know if there are any suggestions
public class AfmServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 7467039855104622536L;
// private static final Log log = LogFactory.getLog(AfmServlet.class);
/**
* @param request The request recieved from the client
* @param response The response to the client
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
//Get the request parameters
String nni = request.getParameter("nni");
String msg = nni+" nni received";
response.sendError(HttpServletResponse.SC_OK, msg);
response.setHeader("nni", "0123456");
response.setHeader("resultCount","5");
// log.debug("Response Headers set");
// File afm_0123456 = null;
ServletOutputStream out = null;
try {
// File f = new File("C:/Documents and Settings/148034/Desktop/new.doc");
String baseName = "afm_0123456.zip";
String fileLocation = findFile(baseName);
File f = new File(fileLocation);
FileInputStream istr = new FileInputStream(fileLocation);
GZIPInputStream ip = new GZIPInputStream(istr);
// log.debug("FileInputStream");
BufferedInputStream bstr = new BufferedInputStream( ip ); // promote
// log.debug("BufferedInputStream");
int size = (int) f.length(); // get the file size (in bytes)
byte[] data = new byte[size]; // allocate byte array of right size
bstr.read( data, 0, size ); // read into byte array
// log.debug("DATA READ");
bstr.close();
// log.debug("BufferedInputStream CLOSED");
response.setContentType("text/html");
//
out = response.getOutputStream();
// log.debug("getOutputStream");
out.write(data);
// log.debug("WRITTEN DATA");
out.flush();
// log.debug("FLUSHED DATA");
out.close();
// afm_0123456 = new File( "afm_0123456.zip" );
// if (afm_0123456.exists()) {
// response.setContentType("text/html");
// RandomAccessFile raf = new RandomAccessFile( afm_0123456, "r" );
// response.setContentLength((int)raf.length());
// out = response.getOutputStream();
// byte [] loader = new byte [ (int) raf.length() ];
// while ( (raf.read( loader )) > 0 ) {
// out.write( loader );
// }
// }else {
// System.out.println("Aborting: File doesn't exists, " + afm_0123456 + ", " + afm_0123456 + File.separator + afm_0123456 +
// "abspath: " + afm_0123456.getAbsolutePath() );
// }
// log.debug("GOING TO DISPATCH IT TO SERVLET");
RequestDispatcher requestdispatcher = request.getRequestDispatcher("/RcuAfmCallbackServlet");
requestdispatcher.forward(request,response);
} catch (IOException ioe) {
// log.debug("Unable to open file "); ioe.printStackTrace();
} finally {
if (out != null) {
out.flush();
out.close();
}
}
}
/**
* @param request The request recieved from the client
* @param response The response to the client
* @throws ServletException
* @throws IOException
*/
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//Call the doGet() method
doGet(request, response);
}
private String findFile(String baseName) {
String absoluteFilename = Utilities.getFileAsPathOrUrl(baseName);
if (absoluteFilename.equals(baseName)) {
absoluteFilename = "C:/btp/Transfer_Engineering_Diagnostics/Construction/Java/project-web/WEB-INF/src/com/bt/oss/trengtd/" + baseName;
}
return absoluteFilename;
}
}