1
votes

I have a spring application in one of the forms the use supposed to upload an excel file.

The application saves the file on the hard desk and provide a link to the user to download it again.

If the file name is written in English every thing goes OK but if the file name contains Arabic characters, the file Arabic characters are converted into question marks.

It is clear that the problem is related to character encoding but I can not detect where is the problem exactly.

Here is the system structure and the configurations:

  • Operating system : Centos
  • Application server : Tomcat
  • connector configs in server.xml

    [Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"]

4
What is your file system's encoding? What is the default encoding of your VM? - PaĆ­lo Ebermann
Try to output the filename to some JSP (using UTF-8) to see if the filename is well received. You should be aware of the client filesystem encoding too. If the browser receives a filename with invalid chars it will change the name. - helios

4 Answers

1
votes
  1. You have to know what's char-set of Arabic character.

  2. If you don't know, you can try with UTF-16.

The code to use is following:

// output stream
ByteArrayOutputStream bout = new ByteArrayOutputStream();

// input stream
InputStream in = new FileInputSteam("filePath");

// reading buffer
byte[] buffer = new byte[1024];

// 1st read
int bytes = in.read(buffer, 0, buffer.length());

while(bytes != -1) {
   // write buffer
   bout.write(buffer);

   // re-load buffer
   bytes = in.read(buffer, bytes, buffer.length());
}

String yourText = bout.toString(Charset.forName("YOUR_CHARSET"));

// close stream or use JSE7 try-catch-with-resource
in.close();
bout.close();

Enjoy yourself.

0
votes

In Windows Control panel, go to Regional Option & in Administrative Tab, select in Langauge of Non Unicode programme, Select the regional Arabic Langauge.

-3
votes

I think its arabic lang not support your system's language so try this.

byte[] utf8Bytes = ("Arabic String").getBytes("arabic"); argument = new Object[]{new String(utf8Bytes,"UTF8")}; System.out.println(argument);