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.