当我尝试上传文件名包含瑞典字符 Ӧ Ӓ å 的文件时,我遇到了编码问题(我猜)。当我在 Windows 上上传文件但在 Mac OS 上上传文件时,Applet 工作正常。
在服务器端(Domino 服务器)打印文件名时会弄乱文件名,并在 Mac 上显示框但是当我通过 new String(filename.getBytes("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
可能是什么问题。
谢谢。
最佳答案
Java 字符串始终在内部编码为 UTF-16,但这与您的问题无关,尝试“设置字符串编码”本质上是错误的。
编码用于在字符串和字节之间转换。您的问题是,在完成此操作的某个时刻,您没有指定编码,因此 Java 使用平台默认编码。
由于文件系统 API 是基于字符串的,所以问题不可能出在那一端,所以 filename
字符串可能在您从用户那里检索它时损坏了 - 或者因为还有另一个错误且毫无意义地尝试“设置 Java 字符串的编码”的实例。
eclipse 中的编码设置仅与您的源代码或项目中的其他文件相关。
关于Java Applet - 无法将默认平台编码更改为其他一些,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6265308/