JSP file download (jar package is not used)

Source: Internet
Author: User

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 }}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.