Dear All,
In my Application i am downloading a file. on button click using servlet.
after clicking the button servlet called and download dialog box opens with open / save and cancel button.

i want to trace these button events.
on open or save i want to update status 'true' in to database and on cancel button i want to update status false for agian download.


to download a file code is given below ..


Code for index page

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
<form method="post" action="DownloadFile">
    <table>
        <tr>
            <td colspan="2">
                <input type="submit" value="Download"> 
            </td>
        </tr>
    </table>
</form>
    </body>
</html>

code of servlet i am calling on button click

/*
 * DownloadFile.java
 *
 * Created on October 20, 2008, 5:46 PM
 */

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author 72737
 * @version
 */
public class DownloadFile extends HttpServlet {

    private String original_filename = "MYFILE.txt";

    private String filename="D:\\ABC.txt";
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
            File                f        = new File(filename);
            int                 length   = 0;
            ServletOutputStream op       = response.getOutputStream();
            ServletContext      context  = getServletConfig().getServletContext();
            String              mimetype = context.getMimeType( filename );
    
            //
            //  Set the response and go!
            //
            //
            response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
            response.setContentLength( (int)f.length() );
            response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
    
            
            //
            //  Stream to the requester.
            //
            byte[] bbuf = new byte[filename.length()];
            DataInputStream in = new DataInputStream(new FileInputStream(f));
    
            while ((in != null) && ((length = in.read(bbuf)) != -1))
            {
                op.write(bbuf,0,length);
            }
    
            in.close();
            op.flush();
            op.close();
    
    
    
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}

Thanks

Some error while downloading..java.io.FileNotFoundException: c:\ABC.txt (The system cannot find the file specified.)

Edited 5 Years Ago by vidyashanmugam: n/a

This article has been dead for over six months. Start a new discussion instead.