Iv. File Download
1、download link page download.html
The page source code is as follows:
<! -- File name: download.html Author: vertical and horizontal software production center Yu Yiqi (zhsoft88@sohu.com) --> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> |
2. Download and processing page do_download.jsp (preferred for SUN Enterprise applications)Do_download.jsp (the preferred choice for SUN enterprise-level applications) demonstrates how to use the SmartUpload component of jsp (the preferred choice for SUN enterprise-level applications) to download files. The following source code shows how simple the download is.
The source code is as follows:
<% @ Page contentType = "text/html; charset = gb2312" import = "com. jsp (preferred for SUN Enterprise Applications) smart. upload. * "%> <% // create a SmartUpload object SmartUpload su = new SmartUpload (); // initialize su. initialize (pageContext); // set contentDisposition to null to prevent the browser from automatically opening the file. // ensure that the file is downloaded after the link is clicked. If this parameter is not set, when the downloaded file extension is // doc, the browser automatically opens it with word. When the extension is pdf, // the browser will open with acrobat. Su. setContentDisposition (null); // download the file su. downloadFile ("/upload/For example, how can I retrieve my first bucket of gold .doc"); %> |
Note that the page for executing the download is out of the Java Script range (that is, <%.... If you do not believe it, you can add a line break between %> <% in the source code and download it again to ensure that an error occurs. It affects the data streams returned to the browser, resulting in parsing errors.
3. How to download Chinese files
Jsp (preferred for SUN Enterprise Applications) SmartUpload can download files, but it does not support Chinese characters. If the downloaded file name contains Chinese characters, the browser displays a bunch of garbled characters when prompted for another file name. The above example is as follows. (This problem also exists in many download components, which is rarely solved and relevant information cannot be found. Sorry !)
To provide support for downloading Chinese files for the SmartUpload component of jsp (preferred for SUN Enterprise applications), I have studied this component, after the UTF-8 code is found to return another file name to the browser, the browser can correctly display the Chinese name. This is a happy discovery. So I upgraded the SmartUpload class of the SmartUpload component of jsp (preferred for SUN enterprise-level applications) and added the toUtf8String method. The source code of some changes is as follows:
Public void downloadFile (String s, String s1, String s2, int I) throws ServletException, IOException, SmartUploadException {if (s = null) throw new IllegalArgumentException ("File" + s + "not found (1040 ). "); if (s. equals ("") throw new IllegalArgumentException ("File" + s + "not found (1040 ). "); if (! IsVirtual (s) & m_denyPhysicalPath) throw new SecurityException ("Physical path is denied (1035 ). "); if (isVirtual (s) s = m_application.getRealPath (s); java. io. file file = new java. io. file (s); FileInputStream fileinputstream = new FileInputStream (file); long l = file. length (); boolean flag = false; int k = 0; byte abyte0 [] = new byte [I]; if (s1 = null) m_response.setContentType ("application/x-msdownload"); e Lseif (s1.length () = 0) m_response.setContentType ("application/x-msdownload"); else response (s1); m_response.setContentLength (int) l); m_contentDisposition = m_contentDisposition! = Null? M_contentDisposition: "attachment;"; if (s2 = null) m_response.setHeader ("Content-Disposition", m_contentDisposition + "filename =" + toUtf8String (getFileName (s ))); elseif (s2.length () = 0) m_response.setHeader ("Content-Disposition", m_contentDisposition); else m_response.setHeader ("Content-Disposition ", m_contentDisposition + "filename =" + toUtf8String (s2); while (long) k <l) {int j = fileinputstream. read (abyte0, 0, I); k + = j; m_response.getOutputStream (). write (abyte0, 0, j);} fileinputstream. close ();}/*** convert the Chinese characters in the file name into UTF-8 encoded strings, so that the saved file name can be correctly displayed during download. * landscape software production center Yu Yiqi 2003.08.01 * @ param s original file name * @ return re-encoded file name */public static String toUtf8String (String s) {StringBuffer sb = new StringBuffer (); for (int I = 0; I <s. length (); I ++) {char c = s. charAt (I); if (c> = 0 & c <= 255) {sb. append (c);} else {byte [] B; try {B = Character. toString (c ). getBytes ("UTF-8");} catch (Exception ex) {System. out. println (ex); B = new byte [0];} for (int j = 0; j <B. length; j ++) {int k = B [j]; if (k <0) k ++ = 256; sb. append ("%" + Integer. toHexString (k ). toUpperCase () ;}} return sb. toString ();} |
Note that in the bold part of the source code, the original jsp (preferred for SUN Enterprise Applications) SmartUpload component does not process the returned files, and now the encoding conversion is done, encodes a file name into a UTF-8. The UTF-8 Code does not take any action on the English language, and the Chinese language needs to be converted to the form of % XX. In toUtf8String method, the UTF-8 encoding of Chinese characters is obtained directly by using the encoding conversion method provided by Java language, and then converted to the form of % XX.