import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
import java.net.URLDecoder;


public class DownloadServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    doAction (request, response);
  }
  
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    doAction (request, response);
  }
  
  
  public void doAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    String fileName = request.getParameter("filename");
    try {
      fileName = URLDecoder.decode(fileName);
    }
    catch (Exception e) {
    }
    String extension = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
    long fileLength = (new java.io.File(fileName)).length();
    System.out.println("\nfileName.length___"+fileLength);
    FileInputStream propFile = new FileInputStream("c:\\temp\\mime.ini");
    Properties p = new Properties();
    FileInputStream in;
    String mimeType; 
    int bytes_read;
    byte[] buffer;
    p.load(propFile);
    mimeType = p.getProperty(extension.toLowerCase());
    propFile.close();
    mimeType = (mimeType == null)? "application/octet-stream": mimeType;
    response.setHeader("Content-Disposition","attachment; filename=" + fileName);
    response.setContentType(mimeType);
    response.setContentLength((int)fileLength);
//	response.setHeader("Accept-Encoding","*");

    in =  new FileInputStream(fileName);
    buffer = new byte[2048];
    ServletOutputStream outStream = response.getOutputStream();
    while ((bytes_read = in.read(buffer)) != -1) {
      outStream.write(buffer, 0 , bytes_read);
    } 
    in.close();
    outStream.flush ();
    outStream.close ();
  }
}