Java Applet - Unable to change default platform encoding to some other
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();
}
开发者_StackOverflow中文版
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.
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.
I just changed the encoding scheme to ISO-8859-1
.
The above mentioned write("Content-Type:");
method was something like this:
1. write(String s){
2. os.write(s.getBytes());
3. }
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) line request.setParameter("Name", new String(tmpAtt.getFileName().getBytes("ISO-8859-1")));
, the Name
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
精彩评论