Tuesday, July 26, 2011

JAVA SERVLET CODE TO DOWNLOAD A .txt file


/*
 * RK
 *
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author rushabh
 */
public class DownloadFile extends HttpServlet {
    protected synchronized void processRequest(
            HttpServletRequest request,
            HttpServletResponse response)
    throws ServletException, IOException {
        String filePath=request.getParameter("filePath");
        System.out.println("filePath = "+filePath);
        String fileName="file1";
       
        if(filePath == null)return;
        if(filePath.contains("/")){
            String[] b = filePath.split("/");
            fileName=b[b.length-1];
        }              
        response.setHeader("Content-Length", String.valueOf(new File(filePath).length()));      
        response.setContentType( "application/octet-stream" );
        //response.setContentType("application/vnd.ms-excel");
               // System.out.println(".......response.getContentType() = "+response.getContentType());
                response.setHeader("Content-Disposition","attachment; filename=\""+fileName+"\"");
        FileInputStream inputStream = null;
        try
        {
        inputStream = new FileInputStream(filePath);
        System.out.println(" INPUTSTREAM CREATED");
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        do{
                bytesRead = inputStream.read(buffer, 0, buffer.length);
                response.getOutputStream().write(buffer);
        }while (bytesRead == buffer.length);
        System.out.println(" END OF WHILE");
        response.getOutputStream().flush();
        System.out.println(" FLUSHING DONE");
        }catch(Exception e){
             System.out.println("Exception in DownloadFile.java ="+e);
        }finally{
        if(inputStream != null)
                inputStream.close();
        System.out.println(" INPUT STREAM CLOSED");
        }
    }
    //
    /**
     * Handles the HTTP GET method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP POST method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }//
}

No comments:

Post a Comment