Java Applet - 无法将默认平台编码更改为其他平台编码
当我尝试上传文件名包含瑞典字符 ٦ ْ å 的文件时,我遇到编码问题(我猜)。当我在 Windows 上上传文件时,Applet 工作正常,但在 Mac OS 上却不行。
当在服务器端(即 Domino 服务器)打印文件名时,文件名会变得混乱,并在 Mac 上显示框,但是当我通过 new String(filename.getBytes("utf-8") 将编码设置为 UTF-8 时))
它在 Win 和 Mac 上都显示 ?
。
更新:
以下是代码片段:
设置请求参数并发布
...
request.setParameter("Name", tmpAtt.getFileName());
...
HttpURLConnection connection ...
connection.setRequestProperty("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + boundary);
if (os == null) os = connection.getOutputStream();
设置文件名和输入流的参数
request.setParameter(fileUploadFieldName, tmpAtt.getFilePath(), fi);
public void setParameter(String name, String filename, InputStream is) throws IOException {
boundary();
writeName(name);
write("; charset=utf-8; filename=\"");
write(filename);
write('"');
newline();
write("Content-Type:");
String type = connection.guessContentTypeFromName(filename);
if (type == null) type = "application/octet-stream";
writeln(type);
newline();
pipe(is, os);
newline();
}
最后发布到服务器
public InputStream post() throws IOException {
boundary();
writeln("--");
printOS(os);
os.close();
InputStream iis = connection.getInputStream();
printIS(iis);
return iis;
}
在 OutputStream 上写入字节以发送请求时获取此输出。名字对我来说看起来不错。
------------------------------hxre3intl6yy-17eufpccwtxc89pbvyg0iwe3i
Content-Disposition: form-data; name="Name
Räpörå.log
------------------------------hxre3intl6yy-17eufpccwtxc89pbvyg0iwe3i
Content-Disposition: form-data; name="Name2
------------------------------hxre3intl6yy-17eufpccwtxc89pbvyg0iwe3i
Content-Disposition: form-data; name="APPROVALSTATUS
可能是什么问题。
谢谢。
I am facing an encoding problem (I guess) when I try to upload a file with file name having swedish charachters Ӧ Ӓ å. Applet works fine when I upload a file on Windows but not on Mac OS.
The filename gets messed up when print it on the server side, which is a Domino server, and shows boxes on Mac but When I set the encoding to UTF-8 by new String(filename.getBytes("utf-8"))
it shows ?
on both Win and Mac.
UPDATED:
Following are the code snippets:
Setting the request params and posting
...
request.setParameter("Name", tmpAtt.getFileName());
...
HttpURLConnection connection ...
connection.setRequestProperty("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + boundary);
if (os == null) os = connection.getOutputStream();
Setting param for filename and Inputstream
request.setParameter(fileUploadFieldName, tmpAtt.getFilePath(), fi);
public void setParameter(String name, String filename, InputStream is) throws IOException {
boundary();
writeName(name);
write("; charset=utf-8; filename=\"");
write(filename);
write('"');
newline();
write("Content-Type:");
String type = connection.guessContentTypeFromName(filename);
if (type == null) type = "application/octet-stream";
writeln(type);
newline();
pipe(is, os);
newline();
}
At the end posting to the server
public InputStream post() throws IOException {
boundary();
writeln("--");
printOS(os);
os.close();
InputStream iis = connection.getInputStream();
printIS(iis);
return iis;
}
Getting this output when writing the bytes on OutputStream to send the request. And name looks fine to me.
------------------------------hxre3intl6yy-17eufpccwtxc89pbvyg0iwe3i
Content-Disposition: form-data; name="Name
Räpörå.log
------------------------------hxre3intl6yy-17eufpccwtxc89pbvyg0iwe3i
Content-Disposition: form-data; name="Name2
------------------------------hxre3intl6yy-17eufpccwtxc89pbvyg0iwe3i
Content-Disposition: form-data; name="APPROVALSTATUS
What could be the problem.
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java字符串始终在内部编码为UTF-16,但这与您的问题无关,并且尝试“设置字符串的编码”本质上是错误的。
编码用于在字符串和字节之间转换。您的问题是,在完成此操作的某个时刻,您没有指定编码,因此 Java 使用平台默认编码。
由于文件系统 API 是基于字符串的,因此问题不可能出现在这一端,因此
filename
字符串可能在您从用户处检索它时已损坏 - 或者因为存在另一个错误的实例并毫无意义地尝试“设置 Java 字符串的编码”。Eclipse 中的编码设置仅与您的源代码或项目中的其他文件相关。
Java strings are always internally encoded as UTF-16, but this is not relevant to your problem, and the attempt to "set the encoding" of a string is inherently wrong.
Encodings are used to translate between Strings and bytes. Your problem is that at some point where this is done, you are not specifying an encoding, so Java uses the platform default encoding.
Since the filesystem API is String-based, the problem cannot be at that end, so the
filename
String is probably corruped at the point where you retrieve it from the user - or because there is another instance of wrongly and pointlessly trying to "set the encoding" of a Java String.The encoding settings in eclipse are only relevant for your source code or other files that are part of your project.
我刚刚将编码方案更改为 ISO-8859-1。
上面提到的 write("Content-Type:"); 方法是这样的:
我只是将第二行更改为 os.write(s.getBytes("ISO-8859- 1"))
它不适用于
UTF-8
,我不知道为什么???该方案已在某处更改为 MacRoman,因为当我将 ISO-8859-1 架构添加到该行(如上所述)时,
request.setParameter("Name", new String(tmpAtt. getFileName().getBytes("ISO-8859-1")));
,Name
末尾出现乱码。但我不明白为什么 UTF-8 不起作用以及为什么该方案在中间某个地方被改变???
谢谢
I just changed the encoding scheme to
ISO-8859-1
.The above mentioned
write("Content-Type:");
method was something like this:And I just changed the 2nd line to
os.write(s.getBytes("ISO-8859-1"))
It did not work with
UTF-8
, I don't know why???the scheme was being changed somewhere to
MacRoman
because when I added the ISO-8859-1 schema to this (as mentioned above) linerequest.setParameter("Name", new String(tmpAtt.getFileName().getBytes("ISO-8859-1")));
, theName
was being garbled at the end.But I could not understand why UTF-8 did not work and why the scheme was being changed somewhere in the middle???
thank-you