简体   繁体   English

在Java中使用Shift_JIS进行日语字符编码

[英]Japanese character encoding using Shift_JIS in Java

I have a web application which is served using tomcat. 我有一个使用tomcat服务的Web应用程序。

On one of the pages, it allows the users to download a file stored on my file server. 在其中一个页面上,它允许用户下载存储在我的文件服务器上的文件。 The names of most of the files present there are in Japanese. 此处显示的大多数文件的名称均为日语。 However, when the user downloads the file, the name of the file is garbled. 但是,当用户下载文件时,文件名会出现乱码。 Also, it works differently on different browsers. 此外,它在不同的浏览器上的工作方式也不同。

The original code is as below: 原始代码如下:

FileInputStream in = new FileInputStream(absolutePath);
ResponseUtil.download(new String(downloadFileName.getBytes("Shift_JIS"), "ISO-8859-1"), in);

eg, 08_タイヨーアクリス_装置開発_実績表 gets interpreted as 例如08_タイヨーアクリス_装置开発_実绩表被解释为
08_ƒ^ƒCƒˆ-[ƒAƒNƒŠƒX_'•'uŠJ”-_ŽÀ-Ñ• in Google Chrome Google Chrome浏览器中的08_ƒ^ ƒCƒˆ- [ƒAƒNƒŠƒX_'•'uŠJ” -_ŽÀ-Ñ•
This problem is due to the presence of '5c' in the file name and seems to be a known problem in Shift_JIS. 此问题是由于文件名中存在'5c'引起的,似乎是Shift_JIS中的已知问题。 I want to know the correct way to work around this problem. 我想知道解决此问题的正确方法。

It looks like the ResponseUtil.download method from the "Seasar sastruts" framework you're using is taking the filename you provide and sticking it directly in the Content-disposition header of the HTTP response it constructs. 您正在使用的“ Seasar sastruts”框架中的ResponseUtil.download方法看起来像是使用您提供的文件名,并将其直接粘贴在其构造的HTTP响应的Content-disposition标头中。

response.setHeader("Content-disposition", "attachment; filename=" + fileName + "\"");

As far as I can tell, HTTP and MIME headers only support ASCII characters, so this technique won't work with non-ASCII characters. 据我所知,HTTP和MIME标头仅支持ASCII字符,因此该技术不适用于非ASCII字符。 (If this is the case, I'd consider it a bug in this class that it unconditionally sticks the filename in to the header.) Modifying or trying to re-encode the string before you pass it in won't work, because this encoding is at a different level. (如果是这种情况,我会认为这是该类中的错误,因为它无条件地将文件名粘贴到标题中。)在传递字符串之前修改或尝试对其进行重新编码将不起作用,因为这编码处于不同级别。

To support non-ASCII characters, the header value needs to be encoded using the MIME encoded-word technique . 为了支持非ASCII字符,标头值需要使用MIME编码字技术进行编码。 There's no way to do this with that ResponseUtil class as it is, because it concatenates the name you provide directly in to a non-encoded-word string. 无法使用该ResponseUtil类直接执行此操作,因为它会将您提供的名称直接连接到非编码字串中。

I think you'll need to rewrite that download() method to check for non-ASCII characters in the filename inputs it receives, and use encoded-word encoding on strings that contain them. 我认为您需要重写该download()方法,以检查它接收到的文件名输入中的非ASCII字符,并对包含它们的字符串使用编码字编码。 You'd want it to look something like this, where some_base64_text is the actual base-64 encoding of the bytes of your file name encoded as Shift-JIS. 您希望它看起来像这样,其中some_base64_text是文件名字节的实际base-64编码,编码为Shift-JIS。 (Or use UTF-8 instead.) (或改用UTF-8。)

Content-disposition: =?Shift_JIS?B?some_base64_text?=

There's probably a lot of different browser behaviors around this, because they're trying to work around various web servers that are doing it "wrong". 围绕这可能有许多不同的浏览器行为,因为它们正尝试解决“错误”的各种Web服务器。 But it looks like encoding it this way is a good bet for getting it working and making it portable. 但是看起来以这种方式编码是使它工作并使其可移植的一个好选择。

Thanks a lot. 非常感谢。 I was able to solve the problem on Chrome using the following: 我可以使用以下方法在Chrome上解决此问题:

ResponseUtil.download(URLEncoder.encode(downloadFileName, "UTF-8"), in);

However, the encoding is still not proper in Firefox and Safari. 但是,在Firefox和Safari中,编码仍然不正确。

In Chrome, the file is named "08_タイヨーアクリス_装置開発_実績表.pdf" But, on Firefox and Safari, it is named "08_%E3%82%BF%E3%82%A4%E3%83%A8%E3%83%BC%E3%82%A2%E3%82%AF%E3%83%AA%E3%82%B9_%E8%A3%85%E7%BD%AE%E9%96%8B%E7%99%BA_%E5%AE%9F%E7%B8%BE%E8%A1%A8.pdf". 在Chrome中,文件名为“ 08_タイヨーアクリス_装置开発_実绩表.pdf”,但在Firefox和Safari中,文件名为“ 08_%E3%82%BF%E3%82%A4%E3%83% A8%E3%83%BC%E3%82%A2%E3%82%AF%E3%83%AA%E3%82%B9_%E8%A3%85%E7%BD%AE%E9%96%8B% E7%99%BA_%E5%AE%9F%E7%B8%BE%E8%A1%A8.pdf”。

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

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