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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18050718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:04:51  来源:igfitidea点击:

UTF-8 Encoding name in downloaded file

javaencodingutf-8

提问by zawhtut

I'm trying to let the user download the excel file with japanese name. It seems that it works IE 8 only and other IE and firefox, it is not working. Kindly suggest me how to hadndle this.

我试图让用户下载日文名称的 excel 文件。似乎它仅适用于 IE 8 和其他 IE 和 firefox,它不起作用。请建议我如何解决这个问题。

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));

回答by Ankur Lathi

Use method setCharacterEncoding:

使用方法setCharacterEncoding

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it. 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; charset=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.

将发送到客户端的响应的字符编码(MIME 字符集)设置为 UTF-8。如果字符编码已由 setContentType(java.lang.String) 或 setLocale(java.util.Locale) 设置,则此方法将覆盖它。用text/html的String调用setContentType(java.lang.String),用UTF-8的String调用这个方法,等价于用text/html的String调用setContentType;字符集=UTF-8。

可以重复调用此方法来更改字符编码。如果在调用 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"));

回答by Tom

回答by zawhtut

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();
    }

回答by kiran

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 );

回答by yngwietiger

Here's what I did, and it works across ALLbrowsers that I tried (Chrome, Firefox and Safari). Also, I didn't have to write any browser-specific code.

这就是我所做的,它适用于我尝试过的所有浏览器(Chrome、Firefox 和 Safari)。此外,我不必编写任何特定于浏览器的代码。

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. This seems to be true for all browsers.

因此,如果浏览器请求以文件名结尾的 URL,它将正确命名文件。这似乎适用于所有浏览器。

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. For example:

就我个人而言,客户端不知道要下载的文件名。我们的系统根据 ID 对文件执行 GET。例如:

/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 redirectto a 2nd API that includes the filename. For example:

所以,我所做的是让该 API 查找文件名(我们将其存储在 db 中,通过 file_id),对其进行 URL 编码,然后重定向到包含文件名的第二个 API。例如:

/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). It also sets the Content-Disposition header to "attachment;" only (DO NOT set the filename. Let the browser get it from the URL.)

注意:第二个 API 忽略文件名(不需要它)。它还将 Content-Disposition 标头设置为“附件”;仅(不要设置文件名。让浏览器从 URL 获取它。)

回答by mingming chen

below works fine.

下面工作正常。

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