0

I've read all the questions which might be similar to this one, but I still have the same problem.

I am trying to send a file to servlet using this code:

<%@page contentType="text/html; charset=ISO-8859-2"%>
...
request.setCharacterEncoding("ISO-8859-2");
...    
<form action="CabServlet" method="POST" enctype="multipart/form-data">
                                    <input type="file" name="cab" id="cab" class="custom-file-input" lang="pl">

And then, reading it on the server side:

response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("ISO-8859-2");
Part filePart = request.getPart("cab");

I know that using ISO-8859-2 encoding is outdated, but it is a pretty old project and I can not modify it ( I can in my files, but I can not change it globally ). I am also new to using JSP and servlet stuff.

The problem which I have is that when I'm receiving a file on Tomcat which is set up on windows everything is ok. But when I try to run my code on Tomcat set on Linux it doesn't work - I receive a file with bad encoding, which makes polish characters "śćź..." appear as "?". The file which I'm sending is a .cab archive, and after reading it and saving it to XML file encoding problems happen.

I am parsing the file from a cab to XML like that:

 DocumentBuilderFactory builderFactory = 
        DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        // Root element
        Element root = doc.createElement("cabparser");
        root.setAttribute("version", getVersion());
        doc.appendChild(root);
        ...
        // Parse XML file to String format and then return it
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        //transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult streamResult = new StreamResult(writer);
        transformer.transform(domSource,streamResult);

        return writer.toString();

Any idea how to fix this?

czetsuya
  • 4,773
  • 13
  • 53
  • 99
Kappucino
  • 1
  • 2
  • It seems you have set the encoding of the respose. [Here](https://stackoverflow.com/questions/6876697/how-to-set-request-encoding-in-tomcat) you can find how could you do that well. In your case I think you would like to read the request body with ISO-8859-2, so you have to chage response.setCharacterEncoding(" ISO-8859-2"); to request.setCharacterEncoding(" ISO-8859-2"); – m4gic Aug 30 '18 at 22:12
  • Thanks for your reply, but it didn't fix my problem. I think I will have to live with that – Kappucino Sep 04 '18 at 07:08
  • There is a second approach you can do: because your encoding settings had no effect, there must be an InputStream that has been read with platform default encoding. You can set this by specifying the file.encoding JVM argument, so in your case you should put the -Dfile.encoding=ISO-8859-2 to your tomcat's JVM arguments in Linux and give it a try. – m4gic Sep 04 '18 at 07:35
  • I can not use JVM argument, but from what I've found I can try to force encoding on each string that I want to. I will try using JVM argument to test it and then try to force encoding on strings. Thanks! – Kappucino Sep 04 '18 at 09:32
  • Yep, it works :) Thanks a lot for your help! :) – Kappucino Sep 04 '18 at 09:49
  • You're welcome :) – m4gic Sep 04 '18 at 09:57

0 Answers0