Jsp file download code sharing

Source: Internet
Author: User

Jsp tutorial file download code sharing

1、download link page download.html

The page source code is as follows:

<! --
File Name: download.html
Author: Landscape software production center Yu Yiqi (zhsoft88@sohu.com)
<! Doctype html public "-// w3c // dtd html 4.01 transitional // en">
<Title> download </title>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312">
<A href = "jsp/do_download.jsp"> click to download </a>


2. the download processing page do_download.jsp shows how to use the jsps tutorial martupload component to download files. The following source code shows how to download files easily.

The source code is as follows:

<% @ Page contenttype = "text/html; charset = gb2312"
Import = "com. jspsmart. upload. *" %> <%
// Create a new smartupload object
Smartupload su = new smartupload ();
// Initialization
Su. initialize (pagecontext );
// Set contentdisposition to null to prevent the browser from automatically opening the file,
// Ensure that the object is downloaded after the link is clicked. If this parameter is not set, the downloaded file name extension is
// When the doc is used, the browser automatically opens it with word. When the extension is pdf,
// The browser will be opened with acrobat.
Su. setcontentdisposition (null );
// Download an object
Su. downloadfile ("/upload/For example, pick up 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

Although jspsmartupload can download files, 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 !)

In order to provide support for downloading Chinese files for the jspsmartupload component, I studied this component and found that after UTF-8 encoding is performed on another file name returned to the browser, the browser can display the Chinese name correctly. This is a happy discovery. So I upgraded the smartupload class of the jspsmartupload component 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 = new java. io. file (s );
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 ");
If (s1.length () = 0)
M_response.setcontenttype ("application/x-msdownload ");
M_response.setcontenttype (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 )));
If (s2.length () = 0)
M_response.setheader ("content-disposition ",
M_contentdisposition );
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 another file name can be correctly displayed during download.
* Landscape software production center Yu Yiqi 2003.08.01
* @ Param s original file name
* @ Return the reencoded 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 jspsmartupload component does not perform any processing on the returned files, and now the encoding is done to convert the file name to the UTF-8 encoding format. UTF-8 encoding is not processed in English, but must be converted to % xx for Chinese. In the toutf8string method, UTF-8 encoding of Chinese characters is obtained directly using the encoding and conversion method provided by the java language, and then converted to the form of % xx.

Compile the source code and package it into jspsmartupload. jar, copy it to the tomcat shared/lib directory (which can be shared by all web applications), and restart the tomcat server to download files containing Chinese names. In addition, the toutf8string method can also be used to convert hyperlinks containing Chinese characters to ensure the link is valid, because some web servers do not support Chinese links.

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.