| | |
JSP download file
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Dec 2007
Posts: 6
Reputation:
Solved Threads: 0
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
code of servlet i am calling on button click
Thanks
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 Syntax (Toggle Plain Text)
<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
Java Syntax (Toggle Plain Text)
/* * 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
Last edited by peter_budo; Oct 20th, 2008 at 4:31 pm. Reason: Keep It Organized - please use [code] tags
Abhilash Narayan
Java Programmer
Java Programmer
![]() |
Similar Threads
- downloading files using jsp (JSP)
- Play music on a JSP page. (JSP)
- how to use jsp and tomcat (JSP)
- For sTyLe: Critical system error; HijackThis log. (Viruses, Spyware and other Nasties)
- Reply - new hijack this file - error came up (Viruses, Spyware and other Nasties)
- hjt log- download. trojan detected (Viruses, Spyware and other Nasties)
- c:\searchpage.html --- mtwirl.dll -- Please help! (Viruses, Spyware and other Nasties)
- Multithreading in jsp-HELP!!! (JSP)
Other Threads in the JSP Forum
- Previous Thread: destroy() method of a servlet class
- Next Thread: How Input parameter insert in DB through Java Bean
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






