Search code examples
servletscharacter-encodingfilenamesapache-commons-fileupload

Unable to read file name of uploaded file as UTF-8


I'm trying to read the video name in servlet, if the video name is in english i can read it fine, if the video name is in arabic i couldn't read it as expected

if (!item.isFormField()) {

                        String value = (String) item.getName();
                        String videoName = new String(
                                value.getBytes("iso-8859-1"), "UTF-8");
                        if (videoName != "")
                            item.write(new File(UPLOAD_DIRECTORY
                                    + videoName));
                        arrayList.add(videoName);

however is working if the item is not form field

else if (item.isFormField()) {
                        String inputName = (String) item.getFieldName();

                            String value = (String) item.getString();
                            value = new String(
                                    value.getBytes("iso-8859-1"), "UTF-8");
                            hashMap.put(inputName, value);

                    }

Solution

  • I have solved the problem by adding req.setCharacterEncoding("UTF-8");