0

Fileupload - how to get raw bytes from a text file ?

asked 2013-08-26 19:21:33 +0800

Jardo gravatar image Jardo
11 1

Hi, I'm building an application, which has to deal with uploaded text files with a whole range of different encodigs. I'm using a fileupload component to upload the files. Here is my zul file:

    ...
    <fileupload label="Upload file" onUpload="@command('importTexts',media=event.media)" />
    ...

... and my view model command:

@Command
@NotifyChange("importedTexts")
public void importTexts(@BindingParam("media") Media media)
{
    ...
}

I have a text file "example.txt" encoded in CP1250, which contains these exact bytes {86,253,112,111,232,101,116}, which represent a valid word in CP1250 charset. When I try to call Media.getByteData(), I get a message saying "use getStringData() instead". But when I do this:

        String string = media.getStringData();
        byte[] bytes = string.getBytes();
        for (int i = 0; i < bytes.length; i++)
        {
            System.out.println(i + ": " + bytes[i]);
        }

then I get these bytes: {86,63,112,111,63,101,116}, so all the special characters got replaced by 63, which is bad.

I noticed that when i rename the exact same file to "example.bin", I can call Media.getByteData(), and I get the right result. Why am I not allowed to call getByteData() just because the file name ends with .txt? This does not make any sense. How can I get pass this - get raw bytes from any file named *.txt?

P.S.: I found out, that when I put this configuration in my zk.xml:

<system-config> 
    <upload-charset>CP1250</upload-charset>
</system-config>

... then also getStringData() works fine. But I cannot use this solution because my app needs to handle all sorts of encodings, not only CP1250. Therefore I'd need to get the exact byte codes of the characters, as they are written on disk. Thanks.

delete flag offensive retag edit
Be the first one to answer this question!
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-08-26 19:21:33 +0800

Seen: 29 times

Last updated: Aug 26 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More