1

I have a webapplication which allows to upload binary files. I have to parse them and save the content 1:1 into a String and then into the database.

When I use uuencode on a unix machine to encode the binary file, then it works. Is there a way to do this automatically in java?

if (isMultipart) {

            //Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload();

            //Parse the request
            FileItemIterator iter = upload.getItemIterator(request);

            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();

                InputStream stream = item.openStream();

                if (!item.isFormField()) {

                    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                    String line;
                    licenseString = "";

                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);

                        // Generate License File
                        licenseString += line + "\n";
                    }
                }
            }
            session.setAttribute("licenseFile", licenseString);
            System.out.println("adding licensestring to session. ");
        }

It works of course for all non-binary files uploaded. How can I extend it to support binary files?

nimrod
  • 5,595
  • 29
  • 85
  • 149

3 Answers3

3
// save to file
// =======================================
InputStream is = new BufferedInputStream(item.openStream());
BufferedOutputStream output = null;

try {
    output = new BufferedOutputStream(new FileOutputStream("temp.txt", false));
    int data = -1;
    while ((data = is.read()) != -1) {
        output.write(data);
    }
} finally {
    is.close();
    output.close();
}

// read content of file
// =======================================
System.out.println("content of file:");
try {
    FileInputStream fstream = new FileInputStream("temp.txt");

    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line;

    licenseString = "";
    String strLine;
    while ((strLine = br.readLine()) != null)   {
          System.out.println(javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()));
          licenseString += javax.xml.bind.DatatypeConverter.printBase64Binary(strLine.getBytes()) + "\n";
    }                                           
} catch (Exception e) {
    System.err.println("Error: " + e.getMessage());
}
nimrod
  • 5,595
  • 29
  • 85
  • 149
1

You could use the commons_fileupload lib (check it here : org.apache.commons.fileupload.disk.DiskFileItem is not created properly?)

The doc is here : http://commons.apache.org/fileupload/using.html Your case is pretty well explained on the official website.

Community
  • 1
  • 1
TecHunter
  • 6,091
  • 2
  • 30
  • 47
0

A Better way would be to write the upload to a temporary file and then handle it from there:

if (!item.isFormField()) {

    InputStream stream = new BufferedInputStream(item.getInputStream());
    BufferedOutputStream output = null;

    try {
        output = new BufferedOutputStream(new FileOutputStream(your_temp_file, false));
        int data = -1;
        while ((data = input.read()) != -1) {
            output.write(data);
        }
    } finally {
        input.close();
        output.close();
    }
}

now you have a temporary file, which is the same as the uploaded file, you can do your 'other' calculations from there.

epoch
  • 16,396
  • 4
  • 43
  • 71
  • thanks, this is a good input, but I already tried this. It does not solve my "encoding"-problem :( – nimrod Feb 21 '12 at 10:42