jan
jan

Reputation: 4063

GWTUpload + AppEngine - Store images as blob - Input stream for file empty?

I had this block of code that worked some time ago. Now it doesn't anymore and I can not really figure out why. I have a FileItem and this seem to have the filename and the correct type and even the size of it seems to be good.

The part where I get the InputStream from the FileItem seems to be the faulty part. If I call the available() method of the stream, it returns 0.

// get input stream from file
imgStream = myFile.getInputStream();
imageBlob = new Blob(IOUtils.toByteArray(imgStream));

Why is this part not working. Is there a way to fix this? The full code looks like this:

import gwtupload.server.exceptions.UploadActionException;
import gwtupload.server.gae.AppEngineUploadAction;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.IOUtils;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.ServingUrlOptions;

/**
 * Upload image to the blob store and return an access url to that image
 *
 */
public class MyUploadAction extends AppEngineUploadAction{

    @Override
    public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {

        logger.info("Saving image to JDO datastore");

        String imageUrls = "";

        // Image service is needed to get url from blob
        ImagesService imagesService = ImagesServiceFactory.getImagesService();

        // Get a file service -> write the blob
        FileService fileService = FileServiceFactory.getFileService();

        // Iterate over the files and upload each one
        for(FileItem myFile : sessionFiles){

            InputStream imgStream = null;
            // construct our entity objects
            Blob imageBlob = null;


            // Create a new Blob file with mime-type "image/png"
            AppEngineFile file = null;

            FileWriteChannel writeChannel = null;
            try {

                // get input stream from file
                imgStream = myFile.getInputStream();
                imageBlob = new Blob(IOUtils.toByteArray(imgStream));


                // create empty app engine file with mime type of uploaded file e.g.: image/png, image/jpeg
                file = fileService.createNewBlobFile(myFile.getContentType(), myFile.getName());

                // Open a channel to write to it
                boolean lock = true;
                writeChannel = fileService.openWriteChannel(file, lock);

                // This time we write to the channel directly
                writeChannel.write(ByteBuffer.wrap(imageBlob.getBytes()));



            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                // Now finalize
                try {
                    writeChannel.closeFinally();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }


            // Get the url from the blob
            ServingUrlOptions suo = ServingUrlOptions.Builder.withBlobKey(fileService.getBlobKey(file)).secureUrl(true);
            imageUrls += imagesService.getServingUrl(suo);
            imageUrls = imageUrls.replaceFirst("0.0.0.0", "127.0.0.1");
        }
        return imageUrls ;
    }
}

Upvotes: 1

Views: 267

Answers (0)

Related Questions