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”;
    }//
}

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s