简体   繁体   English

下载文件中的 UTF-8 编码名称

[英]UTF-8 Encoding name in downloaded file

I'm trying to let the user download the excel file with japanese name.我试图让用户下载日文名称的 excel 文件。 It seems that it works IE 8 only and other IE and firefox, it is not working.似乎它仅适用于 IE 8 和其他 IE 和 firefox,它不起作用。 Kindly suggest me how to hadndle this.请建议我如何解决这个问题。

String fileName = dateString+"_マイページ情報.xls";
byte[] data = writer.getData();
response.setContentType("application/ms-excel");
response.setContentLength(data.length);
response.setHeader("Expires:", "0"); // eliminates browser caching
response.setHeader("Content-Disposition","attachment; filename="+URLEncoder.encode(fileName));

I got it solved as the following.我得到它解决如下。

fileName = dateString+"_マイページ情報.xls"; 
fileName = URLEncoder.encode(fileName,"UTF-8"); 
try {
        response.setContentType("application/ms-excel; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        if(browserType.equals("IE")||browserType.equals("Chrome"))
            response.setHeader("Content-Disposition","attachment; filename="+fileName);
        if(browserType.endsWith("Firefox"))
            response.setHeader("Content-Disposition","attachment; filename*=UTF-8''"+fileName);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

Use method setCharacterEncoding :使用方法setCharacterEncoding

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.将发送到客户端的响应的字符编码(MIME 字符集)设置为 UTF-8。 If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it.如果字符编码已由 setContentType(java.lang.String) 或 setLocale(java.util.Locale) 设置,则此方法将覆盖它。 Calling setContentType(java.lang.String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html;用text/html的String调用setContentType(java.lang.String),用UTF-8的String调用这个方法,等价于用text/html的String调用setContentType; charset=UTF-8.字符集=UTF-8。

This method can be called repeatedly to change the character encoding.可以重复调用此方法来更改字符编码。 This method has no effect if it is called after getWriter has been called or after the response has been committed.如果在调用 getWriter 或提交响应之后调用此方法,则此方法无效。

Modify your code with following:使用以下内容修改您的代码:

response.setContentType("application/ms-excel; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition","attachment; filename="+URLEncoder.encode(fileName, "UTF-8"));

Here's what I did, and it works across ALL browsers that I tried (Chrome, Firefox and Safari).这就是我所做的,它适用于我尝试过的所有浏览器(Chrome、Firefox 和 Safari)。 Also, I didn't have to write any browser-specific code.此外,我不必编写任何特定于浏览器的代码。

According to this link: http://blogs.msdn.com/b/ieinternals/archive/2010/06/07/content-disposition-attachment-and-international-unicode-characters.aspx根据此链接: http : //blogs.msdn.com/b/ieinternals/archive/2010/06/07/content-disposition-attachment-and-international-unicode-characters.aspx

all browsers will attempt to derive the filename from the path component of the URL所有浏览器都将尝试从 URL 的路径部分导出文件名

So, if the browser requests a URL with the filename at the end, it will name the file correctly.因此,如果浏览器请求以文件名结尾的 URL,它将正确命名文件。 This seems to be true for all browsers.这似乎适用于所有浏览器。

In my personal case, the client doesn't know the filename that it wants to download.就我个人而言,客户端不知道要下载的文件名。 Our system does a GET for a file based on an ID.我们的系统根据 ID 对文件执行 GET。 For example:例如:

/api/file/download/<file_id>

So, what I did is to have that API look up the filename (we store it in the db, by file_id), URL-encode it, and redirect to a 2nd API that includes the filename.所以,我所做的是让该 API 查找文件名(我们将其存储在 db 中,通过 file_id),对其进行 URL 编码,然后重定向到包含文件名的第二个 API。 For example:例如:

/api/file/download/<file_id>/<url-encoded filename>

Then, the 2nd API will use the file_id to find and stream back the contents of the file, and the browser will use the filename part to name the downloaded file.然后,第二个 API 将使用 file_id 来查找并流回文件的内容,浏览器将使用文件名部分来命名下载的文件。

NOTE: The 2nd API ignores the filename (doesn't need it).注意:第二个 API 忽略文件名(不需要它)。 It also sets the Content-Disposition header to "attachment;"它还将 Content-Disposition 标头设置为“附件”; only (DO NOT set the filename. Let the browser get it from the URL.)仅(不要设置文件名。让浏览器从 URL 获取它。)

below works fine.下面工作正常。

String fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition","attachment; filename="+fileName );

No need to set setCharacterEncoding and all that just add below line its works fine.无需设置 setCharacterEncoding 和所有只需在行下方添加它的工作正常。

String fileName = URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition","attachment; filename="+fileName );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM