ZK frame File Download file name garbled problem (space, #截断) solve __ garbled problem

Source: Internet
Author: User
Tags java web

In Java Web development, file download feature filename File name garbled problem is often encountered.
for this issue, different browsers, the solution is not the same.
IE, the filename is UTF8 encoded by Urlencoder.
and other browsers (Firefox, Chrome, Safari, Opera) are converted to iso8859-1 by byte.
Therefore, to get the browser type in the background (primarily to identify IE), use a different encoding method

    File Download @SuppressWarnings ({"Unchecked", "deprecation"}) @Command public void FileDownload () throws Unsupp ortedencodingexception {if (checklistitems==null| |
            Checklistitems.size () ==0) {MessageBox.Show ("Please select downloaded file", "hint", Messagebox.ok, messagebox.information);         
        Return
        } iterator It=checklistitems.iterator ();
        Map<string,object> selectmap= (MAP) it.next ();
        String filepath=selectmap.get ("File_path") + "";
        String fileextension=filepath.substring (Filepath.lastindexof ("."));
        String Encodename= (String) selectmap.get ("file_name");
        if (!encodename.endswith (fileextension)) {encodename=encodename+fileextension; ///file garbled problem compatible ie httpservletrequest request = (HttpServletRequest) executions.getcurrent (). Getnativerequest ()        
        ;
        String Header = Request.getheader ("User-agent"). toUpperCase (); if (Header.contains ("MSIE") | | Header.contains ("TRIDENT") | |
            Header.contains ("EDGE")) {EncodeName = Urlencoder.encode (EncodeName, "utf-8");    EncodeName = Encodename.replace ("+", "%20");    ie download filename space variable + number problem EncodeName = Encodename.replace ("#", "%23"); Encountered a # truncation problem} else {encodename=new String (encodename.getbytes ("UTF-8"), "iso-8859-1")
            ; EncodeName = Encodename.replace ("#", "%23");//This setting is unsuccessful} file File=new file (Misutils.getwebrootdir () +file              
        Path);
        InputStream Is=null;
        try {is = new FileInputStream (file);
            catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); MessageBox.Show ("no corresponding file found.")
            "," Error ", Messagebox.ok, Messagebox.error);         
        Return 
            } if (File.exists ()) {//filedownload. Save (IS, null, encodename); Amedia media=new Amedia (enCodename, Fileextension.substring (1), NULL, is); Clients.response (New Audownload (Executions.getcurrent (). Getdesktop (). Getdownloadmediauri (Media, EnCod
        ename). Replace ("+", "%2b"). Replace ("#", "%23"));
 }
    }

To this IE browser can easily resolve file name contains # or space to the + number problem
However, browsers such as chrome use a different encoding method iso8859-1
This encoding is not on + or # encoding, causing the server to download the background or the + or # as a special symbol processing, resulting in the download file name is not correct or truncated.

The hexadecimal encoding of the relevant special characters is attached here

The URL appears with +, space,/,?,%,#,&,= and other special symbols, may not get the correct parameter values on the server side, how is good.
Solutions
Converts these characters into characters that the server can recognize, and the corresponding relationships are as follows:
URL character escape
Replace it with a different character, or with a full angle.
+ the + number in the URL indicates the space%2b
Spaces in a space URL can be%20 with a + number or code
/delimited directory and subdirectory%2f
? Separating the actual URLs and parameters%3f
% Specify special characters%25
# indicates bookmark%23
& Separator between parameters specified in the URL%26
= The value of the specified parameter in the URL%3d

Because the use of chrome download is iso-8859-1 # and + and is not encoded so use filedownload. Save (IS, null, encodename); The server or the decoding of special symbols caused an error
To solve this problem, new methods are needed to encode special characters (note that #replace to%23 is unsuccessful after iso8859-1 encoding)
Resolved as follows (replace FileDownload with the following code):

EncodeName is the filename, the second parameter is the file name extension such as TXT, the third parameter is contentype such as text/plain can use NULL (take the default), fourth is the file's stream
Amedia media=new Amedia ( EncodeName, fileextension.substring (1), NULL, is);
Replaces the special characters contained in the URI of the download with the
clients.response (New Audownload () executions.getcurrent (). Getdesktop (). Getdownloadmediauri (
                    Media, EncodeName). Replace ("+", "%2b"). Replace ("#", "%23"));

This raises the question of how to solve this problem if ordinary javaweb (not ZK) use non-IE downloads. (

    Response.setheader ("Content-disposition", "attachment; Filename=\ "" + fileName + "\");
        Response.setheader ("Content-length", String.valueof (Filelength));

Reference:
http://blog.csdn.net/lisehouniao/article/details/52550539
https://stackoverflow.com/questions/47549771/zk8-filedownload-save-cut-filenames#

Thanks to the ZK's Jean yen, here for your help, and I wish the ZK getting better
Hi,
hungly

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.