0

I'm using PrimeFaces 5.1 and p:fileUpload component for uploading images. But I got undefined chrachters for Turkish chracters(for example "ı ç ş ğ"). I researched and tried a lot of wat but I couldn't succeed.I saw this question but not solved my problem. My char encoding filter like below. I also defined filter for this in web.xml file.

public class CharacterEncodingFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
            req.setCharacterEncoding("ISO-8859-9");
            resp.setCharacterEncoding("ISO-8859-9");
            chain.doFilter(req, resp);
    }

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void destroy() {

    }}

My handleFileUpload method

public void handleFileUpload(FileUploadEvent event) {
try {
System.out.println(new String(event.getFile().getFileName().getBytes("UTF-8")));
System.out.println(new String(event.getFile().getFileName().getBytes("ISO-8859-9")));
} catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

I'm getting this file names to UTF8 and ISO-8859-9 charsets for "Adsız.png".

Adsız.png

Adsız.png

Community
  • 1
  • 1
Haktan Aydın
  • 581
  • 1
  • 6
  • 21
  • 1
    Please never use `new String(...getBytes(...))`. Those are workarounds, not solutions. First of all, what do you see when you print the file name straight away like `System.out.println(event.getFile().getFileName());` and when you print the hardcoded value like `System.out.println("Adsız.png");`? – BalusC Mar 25 '15 at 16:42
  • Looks like it should be in console Adsız.png – Haktan Aydın Mar 25 '15 at 16:48
  • System.out.println(event.getFile().getFileName()); output : Adsız.png System.out.println("Adsız.png"); output : Adsız.png – Haktan Aydın Mar 25 '15 at 16:51
  • 1
    OK, the data is originally submitted using UTF-8 (which is actually also the default encoding as used by JSF2+Facelets), but you're telling the server that it's ISO-8859-9. Why? You need to set request character encoding back to UTF-8 in the filter. Give it a try and let me know. By the way, you should not touch response character encoding there at all. – BalusC Mar 25 '15 at 16:54
  • I changed character encoding back to UTF-8 but still getting same outputs Adsız.png and Adsız.png – Haktan Aydın Mar 25 '15 at 17:02
  • I realized that shortly before My tomcat encoding is Cp1254. Is it related with it @BalusC? – Haktan Aydın Mar 25 '15 at 17:06
  • 2
    OK, are you using commons or native file upload? There is an encoding bug when using commons fileupload with PrimeFaces. See also a.o. http://stackoverflow.com/questions/11190081/primefaces-fileupload-filter-with-utf8-characters-filter and https://code.google.com/p/primefaces/issues/detail?id=3002 With PF 5.x and JSF 2.2 you should be able to use native file upload. – BalusC Mar 25 '15 at 17:39

2 Answers2

0

Thank you @BalusC for your valuable comments. I changed to Tomcat encoding Cp1254 to UTF-8 and I confirm that I've used native PrimeFaces library and solved the problem. Thanks @BalusC again.

Haktan Aydın
  • 581
  • 1
  • 6
  • 21
-2

Can you checking this topic. try this code. Java Uploaded File Name

Web.xml

<filter>
    <filter-name>Character Encoding Filter</filter-name>
    <filter-class>org.primefaces.examples.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Character Encoding Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
Community
  • 1
  • 1
Ömer Faruk Kurt
  • 500
  • 3
  • 14