views:

21

answers:

1

I'm using GwtUpload to upload images on GAE. The problem is that the images seem to get much bigger when I move them into a blob. I've noticed the same for simple text fields and in that case I've realised that some weirdo characters are being appended to the end of the field values (encoding right?). Can anyone help?

public class ImageUploadServlet extends AppEngineUploadAction {

/**
 * Maintain a list with received files and their content types
 */
Hashtable<String, File> receivedFiles = new Hashtable<String, File>();
Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();

private Objectify objfy;

public ImageUploadServlet() {

    ObjectifyService.register(Thumbnail.class);
    objfy = ObjectifyService.begin();

    System.out.println("ImageUploadServlet init");
}

/**
 * Override executeAction to save the received files in a custom place and
 * delete this items from session.
 */
@Override
public String executeAction(HttpServletRequest request,
        List<FileItem> sessionFiles) throws UploadActionException {

    Thumbnail t = new Thumbnail();

    for (FileItem item : sessionFiles) {
        //CacheableFileItem item = (CacheableFileItem)fItem;

        if (false == item.isFormField()) {
            System.out.println("the name 1st:" + item.getFieldName());
            try {
                // You can also specify the temporary folder
                InputStream imgStream = item.getInputStream();
                Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));

                t.setMainImage(imageBlob);

                System.out.println("blob: " + t.getMainImage());

            } catch (Exception e) {
                throw new UploadActionException(e.getMessage());
            }

        } else {

            System.out.println("the name 2nd:" + item.getFieldName());

            String name = item.getFieldName();
            String value;
            try {
                InputStream is = item.getInputStream();

                StringWriter writer = new StringWriter();

                IOUtils.copy(is, writer,"UTF-8");

                value = writer.toString(); 
                writer.close();

                System.out.println("parm name: " + name);
                System.out.println("parm value: " + value + " **" + value.length());
                System.out.println(item.getContentType());

                if (name.equals("thumb-name")) {
                    t.setName(value);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("Error");
                e.printStackTrace();
            }
        }

        removeSessionFileItems(request);
    }

    objfy.put(t);

    return null;
}

As an example the size of the image is 20kb, and the lib has some debugging that confirms that this is the case when it's uploading the file but the blob ends up being over 1 MB.

Wes

A: 

Hi Wes, Did you ever resolve this issue? I seem to be running in to the same problem.

John