Jspsmartupload Upload full Raiders 2

Source: Internet
Author: User
Tags add object file upload header string tostring root directory stringbuffer
js| Raiders | upload | download C. Common ways to download files

1, Setcontentdisposition

Action: Appends data to the Content-disposition field of the MIME file header. The Jspsmartupload component automatically fills in the Content-disposition field of the MIME file header when the downloaded information is returned, using this method if the user needs to add additional information.

Prototype: public void Setcontentdisposition (String contentdisposition)

Where Contentdisposition is the data that you want to add. If contentdisposition is null, the component will automatically add "attachment" to indicate that the downloaded file is an attachment, and the result is that IE will prompt for the save file. Instead of automatically opening this file (ie browser generally determines what to do based on the file name extension you download, the extension doc will be opened with a Word program, the extension PDF will be opened with Acrobat program, and so on).

2, DownloadFile

function: Download the file.

Prototype: The following three prototypes are available, the first one is most commonly used, and the last two are used for file downloads in special cases (such as changing the content type, changing the saved file name).

①public void DownloadFile (String sourcefilepathname)

Where Sourcefilepathname is the name of the file to download (full name of the file with the directory)

②public void DownloadFile (String sourcefilepathname,string contentType)

Where Sourcefilepathname is the name of the file to be downloaded (the full name of the file with the directory), ContentType is the content type (MIME-formatted file type information, which can be identified by the browser).

③public void DownloadFile (String sourcefilepathname,string contenttype,string destfilename)

Where Sourcefilepathname is the name of the file to download (the full name of the file with the directory), ContentType is the content type (MIME format file type information, can be identified by the browser), destFileName is the default save file name after downloading.

third, file upload Chapter

㈠ Form Requirements

There are two requirements for the form form for uploading files:

1, Method application post, that is, method= "post."

2, add attributes: Enctype= "Multipart/form-data"

Here is an example of the form form used to upload a file:

     
      
       
      <form method= "POST" enctype= "Multipart/form-data" action= "/jspsmartupload/upload.jsp" ><INPUT "FILE" Name= "MYFILE" ><input type= "SUBMIT" ></FORM>
     
      


examples of ㈡ uploads

1, upload page upload.html

This page provides the form, lets the user select to upload the file, clicks "Uploads" the button to carry on the upload operation.

Page source code is as follows:

       
        
<!--filename: upload.html Author: Vertical and horizontal software production center rain also odd (zhsoft88@sohu.com)--><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


2, Upload processing page do_upload.jsp

This page performs file upload operations. The source of the page details the use of the upload method, this is not to repeat.

Page source code is as follows:

        
         
<%--FileName: do_upload.jsp Author: Vertical and horizontal software production center rain also odd (zhsoft88@sohu.com)--%><%@ page contenttype= "text/html; charset=gb2312 "language=" java "import=" java.util.*,com.jspsmart.upload.* "errorpage=" "%>


Four, download the paper

1, download link page download.html

Page source code is as follows:

     
         
          
      <!--filename: download.html  : Vertical and horizontal software production center rain also odd (zhsoft88@sohu.com)--><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


2, download the processing page do_download.jspDo_download.jsp shows how to use the Jspsmartupload component to download files, from the following source can be seen, download how simple.

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 ();//Initialize Su.initialize (PageContext); Set contentdisposition NULL to prevent the browser from automatically opening the file,//guaranteed to click on the link is the download file. If not set, the browser will automatically open it in Word when the downloaded file name extension is//doc. When the extension is PDF, the//browser opens with Acrobat. Su.setcontentdisposition (null);//download File Su.downloadfile ("/upload/How to earn my first bucket of gold. Doc");%>
     
          


Note that the downloaded page, outside of the Java script (that is, <% ...%>), does not contain HTML code, spaces, carriage returns, or newline characters, and some words will not be downloaded correctly. If not, you can add a line break between%><% in the above source code, and then download it to ensure error. Because it affects the data stream returned to the browser, it causes parsing errors.

3. How to download Chinese files

Jspsmartupload can download files, but not enough support for Chinese. If the file name of the download has Chinese characters, then the browser prompts the saved file name, the display is a pile of garbled, very sweeping people hing. The example above is this. (This problem is also a large number of download components of the problem, very few people solve, search does not find relevant information, alas!) )

In order to increase the support for downloading Chinese files for jspsmartupload components, I have studied this component and found that the browser can display the Chinese name correctly after UTF-8 encoding the saved file name returned to the browser. This is a delightful discovery. So I have to Jspsmartupload component Smartupload class did the upgrade processing, added toutf8string This method, the change part source code 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 Aby te0[] = new Byte[i];if (S1 = = null) m_response.setcontenttype ("Application/x-msdownload"); ElseIf (s1.length () = 0) m_ Response.setcontenttype ("Application/x-msdownload"); else 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)) ); ElseIf (s2.length () = 0) M_response.setheader ("Content-disposition", m_contentdisposition); else M_response.set Header ("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 ();     /** * Converts the Chinese character in the file name into a UTF8 encoded string so that the saved file name is displayed correctly when downloading. * Vertical and horizontal software production center rain also odd 2003.08.01 * @param s original filename * @return recoding filename/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 The bold part of the source code, the original Jspsmartupload component of the returned file did not do any processing, and now do the conversion of the code to convert the file name to UTF-8 form of the encoding form. The UTF-8 code does not have any processing in English, and the Chinese language needs to be converted to the%xx form. In the Toutf8string method, the UTF-8 encoding of Chinese characters is obtained directly by using the encoding conversion method provided by the Java language, and then converted to the%xx form.

After compiling the source code into Jspsmartupload.jar, copy it to Tomcat's Shared/lib directory (which can be shared by all Web applications), and then restart the Tomcat server to download the file containing the Chinese name normally. Alternatively, the Toutf8string method can be used to convert hyperlinks that contain Chinese to ensure that the links are valid because some Web servers do not support Chinese links.

Summary: Jspsmartupload component is the application of JSP for B/S program development process often used in the upload download components, it is simple to use, convenient. Now I have added a file to download the Chinese name of the support, this is a powerful, will win more developers of all ages.

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.