JSP file download (jar package is not used)
Java code:
Public class Download extends HttpServlet {public Download () {super ();} public void destroy () {super. destroy (); // Just puts destroy string in log // Put your code here} public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this. doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) Throws ServletException, IOException {String path = D:/upload/harhat.txt; try {// path refers to the path of the file to be downloaded. File file = new File (path); // get the File name. String filename = file. getName (); // get the file suffix. String ext = filename. substring (filename. lastIndexOf (.) + 1). toUpperCase (); // download the object as a stream. InputStream FCM = new BufferedInputStream (new FileInputStream (path); byte [] buffer = new byte [FCM. available ()]; FCM. read (buffer); FCM. close (); // clear response. reset (); // set the response Header response. addHeader (Content-Disposition, attachment; filename = + new String (filename. getBytes (gb2312), ISO8859-1); response. addHeader (Content-Length, + file. length (); OutputStream toClient = new BufferedOutputStream (response. getOutputStream (); response. setContentType (application/octet-stream); toClient. write (buffer); toClient. flush (); toClient. close ();} catch (IOException ex) {ex. printStackTrace () ;}} public void init () throws ServletException {// Put your code here }}