Jsp file download function implementation code, jsp file download code

Source: Internet
Author: User

Jsp file download function implementation code, jsp file download code

This article provides three methods for implementing the file download function in jsp for your reference. The details are as follows:

First, adopt the forwarding method:

Package cn. jbit. download. servlet; import java. io. IOException; import javax. servlet. requestDispatcher; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class DownloadServlet extends HttpServlet {private static final long serialVersionUID = 671_85208899952414l; public voi D doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, response) throws ServletException, IOException {String filedownload = "/upload/1/10213.jpg"; // The relative path of the file to be downloaded, String filedisplay =" 10213.jpg"; // The file storage name response displayed when the file is downloaded. setContentType ("application/x-do Wnload "); // set to download application/x-download // response. setContentType ("application/x-msdownload"); // set to download application/x-msdownload // response. setContentType ("application/octet-stream"); // set to download application/octet-stream response. addHeader ("Content-Disposition", "attachment; filename =" + filedisplay); try {RequestDispatcher rd = request. getRequestDispatcher (filedownload); if (rd! = Null) {rd. forward (request, response) ;}response. flushBuffer () ;}catch (Exception e) {e. printStackTrace ();}}}

Ii. output stream mode:

package cn.jbit.download.servlet;  import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream;  import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  public class DownloadOfIOServlet extends HttpServlet {  public void doGet(HttpServletRequest request, HttpServletResponse response)       throws ServletException, IOException {     doPost(request, response);   }      public void doPost(HttpServletRequest request, HttpServletResponse response)       throws ServletException, IOException {     String basePath = request.getSession().getServletContext().getRealPath("/upload");      String filedisplay = "helloworld.jpg";     String filedownload = basePath + File.separator + "helloworld.jpg";     response.setContentType("applicaiton/x-download");     response.addHeader("Content-Disposition", "attachment;filename="+filedisplay);          InputStream is = null;     OutputStream os = null;     BufferedInputStream bis = null;     BufferedOutputStream bos = null;          is = new FileInputStream(new File(filedownload));     bis = new BufferedInputStream(is);     os = response.getOutputStream();     bos = new BufferedOutputStream(os);          byte[] b = new byte[1024];     int len = 0;     while((len = bis.read(b)) != -1){       bos.write(b,0,len);     }          bis.close();     is.close();     bos.close();     os.close();   } } 

Third, through the hyperlink (note that it is not recommended because it will expose the location of the downloaded file)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.