JSP download file

Reply

Join Date: Dec 2007
Posts: 6
Reputation: narayanabhilash is an unknown quantity at this point 
Solved Threads: 0
narayanabhilash narayanabhilash is offline Offline
Newbie Poster

Re: JSPs and Servlets

 
0
  #1
Oct 20th, 2008
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

  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  4. <title>JSP Page</title>
  5. </head>
  6. <body>
  7. <form method="post" action="DownloadFile">
  8. <table>
  9. <tr>
  10. <td colspan="2">
  11. <input type="submit" value="Download">
  12. </td>
  13. </tr>
  14. </table>
  15. </form>
  16. </body>
  17. </html>


code of servlet i am calling on button click

  1. /*
  2.  * DownloadFile.java
  3.  *
  4.  * Created on October 20, 2008, 5:46 PM
  5.  */
  6.  
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12.  
  13. /**
  14.  *
  15.  * @author 72737
  16.  * @version
  17.  */
  18. public class DownloadFile extends HttpServlet {
  19.  
  20. private String original_filename = "MYFILE.txt";
  21.  
  22. private String filename="D:\\ABC.txt";
  23.  
  24. /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  25.   * @param request servlet request
  26.   * @param response servlet response
  27.   */
  28. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  29. throws ServletException, IOException {
  30.  
  31. File f = new File(filename);
  32. int length = 0;
  33. ServletOutputStream op = response.getOutputStream();
  34. ServletContext context = getServletConfig().getServletContext();
  35. String mimetype = context.getMimeType( filename );
  36.  
  37. //
  38. // Set the response and go!
  39. //
  40. //
  41. response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
  42. response.setContentLength( (int)f.length() );
  43. response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
  44.  
  45.  
  46. //
  47. // Stream to the requester.
  48. //
  49. byte[] bbuf = new byte[filename.length()];
  50. DataInputStream in = new DataInputStream(new FileInputStream(f));
  51.  
  52. while ((in != null) && ((length = in.read(bbuf)) != -1))
  53. {
  54. op.write(bbuf,0,length);
  55. }
  56.  
  57. in.close();
  58. op.flush();
  59. op.close();
  60.  
  61.  
  62.  
  63. }
  64.  
  65. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  66. /** Handles the HTTP <code>GET</code> method.
  67.   * @param request servlet request
  68.   * @param response servlet response
  69.   */
  70. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  71. throws ServletException, IOException {
  72. processRequest(request, response);
  73. }
  74.  
  75. /** Handles the HTTP <code>POST</code> method.
  76.   * @param request servlet request
  77.   * @param response servlet response
  78.   */
  79. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  80. throws ServletException, IOException {
  81. processRequest(request, response);
  82. }
  83.  
  84. /** Returns a short description of the servlet.
  85.   */
  86. public String getServletInfo() {
  87. return "Short description";
  88. }
  89. // </editor-fold>
  90. }



Thanks
Last edited by peter_budo; Oct 20th, 2008 at 4:31 pm. Reason: Keep It Organized - please use [code] tags
Abhilash Narayan
Java Programmer
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,616
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: JSP download file

 
0
  #2
Oct 22nd, 2008
> i want to trace these button events.

You can't trap this event.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JSP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC