Reputation: 6466
I'm trying to get file name from request header, I'm getting character encoding problem despite I have defined Spring encoding filter in my web.xml.
String fileName = request.getHeader("X-File-Name"); // wrong encoding
web.xml
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Also I've added URIEncoding="UTF-8"
config into Tomcat server.xml file.
And added same config JAVA_OPTS too.
-DuriEncoding=UTF-8 -Dfile.encoding=UTF-8
Upvotes: 0
Views: 4980
Reputation: 140220
Well if the headers are always in UTF-8 (or ascii otherwise), you could do this:
public static String reEncode( String input ) {
Charset w1252 = Charset.forName("Windows-1252"); //Superset of ISO-8859-1
Charset utf8 = Charset.forName("UTF-8");
return new String(input.getBytes(w1252), utf8 );
}
...
String fileName = reEncode("Mekanizması.pdf"); //request.getHeader("X-File-Name")
System.out.println(fileName); //Mekanizması.pdf
Upvotes: 1
Reputation: 18405
There is no encoding defined for HTTP headers. Expect them to support at most ISO-8859-1. Better rely on US-ASCII. The param is, as the name says, for URIs and their params. Nothing else.
Upvotes: 0