0

I have some JavaScript code that upload file to server using ajax and form data and server side java code that accept it. I can upload English file name. But when I uploaded other Unicode file name, the file name I got in server side is unreadable. The following is code snippet.

JavaScript

 var f = new FormData();
 f.append("file", file);
 xhr.send(f);

Java

public void upload(MultipartFormDataInput input) {
        Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
        List<InputPart> inputParts = uploadForm.get("user_file[]");
        IFileInfo file = null;
        for (InputPart inputPart : inputParts) {            
            try {               
                MultivaluedMap<String, String> header = inputPart.getHeaders();
                fileName = getFileName(header);
                System.out.println("File name is " + fileName);
            } catch (IOException e) {
                e.printStackTrace();
            }           
        }
    }

private String getFileName(MultivaluedMap<String, String> header) {
        System.out.println("Headers is " + header.getFirst("Content-Disposition"));
        String[] contentDisposition = header.getFirst("Content-Disposition")
                .split(";");

        for (String filename : contentDisposition) {
            if ((filename.trim().startsWith("filename"))) {

                String[] name = filename.split("=");    
                String finalFileName = name[1].trim().replaceAll("\"", "");

                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }                   
                return finalFileName;
            }
        }
        return "unknown";
    }

When I upload "大家好.jpg" , I got server side log showing the following.

Headers is form-data; name="user_file[]"; filename="���������.jpg"
File name is ���������.jpg

I think browser encode file name before uploading it.But I don't know which encoding did it used or how to decode it back. Any help is much appreciated.

FranXho
  • 736
  • 7
  • 18
  • _Did_ the file upload correctly though? Because it may just be that everything worked but the program you are using to _view the log_ cannot display the Japanese characters. (Oh and by the way, you might want to change the words "Japanese file name or other Unicode file name" because English letters are Unicode characters too, just like Japanese characters. :-) ) – Ray Toal Jul 29 '14 at 05:58
  • In fact, the file is uploaded with these wrong "���������.jpg" name.By the way, thank for correcting. – FranXho Jul 29 '14 at 06:32
  • 1
    To see what the browser sends, fire up wireshark and follow the HTTP session. – n. m. could be an AI Jul 29 '14 at 06:47
  • @Franxo try wireshark (or similar tool) as n.m. suggests to see what the browser is sending. Hopefully the browser knows how to encode UTF-8 and the bytes for 大家好 will be e5 a4 a7 e5 ae b6 e5 a5 bd. If it is, the problem is with the server being unable to decode. – Ray Toal Jul 29 '14 at 08:01

0 Answers0