Jsp implementation Servlet File Download Method, jspservlet File Download
This document describes how to download Servlet files in jsp. Share it with you for your reference. The details are as follows:
Package com; import java. io. IOException; import java. io. printWriter; import java.net. URLEncoder; import java. util. date; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; import com. sun. xml. internal. messagin G. saaj. packaging. mime. internet. MimeUtility;/*** File Download class. To prevent the client browser from directly opening the target file (for example, in Windows with MS Office suite installed, ie may directly open the doc or xls file you want to download in IE ), add the forced download MIME type to the response header. */Public class DownloadFile extends HttpServlet {private static final Log = LogFactory. getLog (DownloadFile. class); public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException {long timeStart = 0; if (log. isDebugEnabled () {timeStart = System. currentTimeMillis ();} response. setContentType ("application/x-download charset = UTF-8"); java. io. fileInputStream = Null; String filepath = request. getRealPath (""); javax. servlet. servletOutputStream sos = null; // System. out. println ("DownloadFile filename:" + filename); try {if (request. getParameter ("filename") = null | request. getParameter ("showName") = null) {return;} String filename = request. getParameter ("filename"); String showName = request. getParameter ("showName"); request. setCharacterEncoding ("UTF-8 "); Response. setCharacterEncoding ("UTF-8"); java. io. File file = new java. io. File (filepath + filename); if (! File. exists () {log. error (file. getAbsolutePath () + "the file does not exist! "); Return;} // read the file stream. io. fileInputStream (file); // you can specify the file name sos = response. getOutputStream (); showName + = filename. substring (filename. lastIndexOf (". "); String contentDisposition =" ", browser = getBrowser (request); if (" IE ". equals (browser) {contentDisposition = "attachment; filename =" + URLEncoder. encode (showName, "UTF-8 "). replace ("+", "% 20");} else if ("CH ". equals (browser )){ ContentDisposition = "attachment; filename =" + MimeUtility. encodeText (showName, "UTF8", "B");} else if ("SF ". equals (browser) {contentDisposition = "attachment; filename =" + new String (showName. getBytes ("UTF-8"), "ISO8859-1");} else {contentDisposition = "attachment; filename * = UTF-8'' "+ URLEncoder. encode (showName, "UTF-8 "). replace ("+", "% 20");} response. setHeader ("Content-Disposition", cont EntDisposition); int byteCount = 0; if (FS! = Null) {byte [] buff = new byte [1024]; int bytesRead; while (-1! = (BytesRead = Fi. read (buff, 0, buff. length) {sos. write (buff, 0, bytesRead); sos. flush (); byteCount + = bytesRead;} sos. flush (); if (log. isDebugEnabled () {log. debug ("file download completed, file size:" + byteCount + ", total time:" + (new Date (). getTime ()-timeStart) + "millisecond. ") ;}} Catch (IOException ioe) {ioe. printStackTrace () ;}finally {try {if (FCM! = Null) {FCM. close () ;}catch (IOException e) {} finally {try {if (sos! = Null) {sos. close () ;}} catch (IOException e) {}}} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/html"); PrintWriter out = response. getWriter (); out. println ("<! DOCTYPE html PUBLIC \ "-// W3C // dtd xhtml 1.0 Transitional // EN \"\" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd \ ">"); Out. println ("
I hope this article will help you with JSP program design.