GWTUpload + AppEngine - 将图像存储为blob - 文件的输入流为空?

时间:2014-08-27 13:24:37

标签: java google-app-engine gwt blob gwtupload

我有一段时间以前工作过的代码块。现在它已经不存在了,我无法弄清楚原因。我有一个FileItem,这似乎有文件名和正确的类型,甚至它的大小似乎是好的。

我从InputStream获得FileItem的部分似乎是错误的部分。如果我调用流的available()方法,则返回0

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

为什么这部分不起作用。有没有办法来解决这个问题?完整代码如下所示:

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 ;
    }
}

0 个答案:

没有答案