sathya
sathya

Reputation: 384

how to get uploaded file as blobs in servlet using google app engine

I can use the below code to upload a file as blobs,

<form action="<%= blobstoreService.createUploadUrl("/upload") %>" method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Submit">
</form>

but in my case am using the Apache Common File Uploader to upload a file. So my form action will be as below,

<form action="/upload" method="post" enctype="multipart/form-data">

now in my servlet am getting the file as InputStream. if i want to convert the file as blob in the same servlet file how can i convert it. Kindly suggest me an idea.

UPDATED

I tried with the Writing files to blob store my code is as follows,

FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime,fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1 = is.read(b1, 0, BUFFER_SIZE);
while (readBytes1 != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, BUFFER_SIZE));}

Now i can upload the file in app engine, it stores the blob values but i cant view the blob values. When i use <%= blobstoreService.createUploadUrl("/upload") %> this line my app engine shows me "View Blob" option but now its not there.Also when i use the blob list to view the file its not showing and says "0" bytes. I think it just stores the blob key but not the file. Kindly suggest me an idea to solve it.

Upvotes: 0

Views: 3552

Answers (2)

Peter Knego
Peter Knego

Reputation: 80340

There are two ways to do this:

  1. Use provided blobstore upload functionality: your client calls server which replies with one-time upload url (created via blobstoreService.createUploadUrl("/upload")). Then you can use this url one-time-only with your Apache Common File Uploader.

  2. Handle uploads in your code: create a multipart handler and then save data to blobstore programmatically via Files API.

Note that option 2. falls under standard GAE 32Mb request/response size limit. Option 1. has a 2Gb file upload limit.

Upvotes: 3

Moritz Petersen
Moritz Petersen

Reputation: 13057

Do something like this:

        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(maxPostSize);

        for (FileItemIterator it = upload.getItemIterator(request); it.hasNext();) {
            FileItemStream item = it.next();
            final byte[] fileData = IOUtils.toByteArray(item.openStream());

            // store data

        }

Now store the fileData in the datastore. Make sure, you check if the item is a form field using item.isFormField().

Or check out the Streaming API.

Storing the file in the Blobstore is done like this, assuming you have the form field <input type="file" name="myFile">:

Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
BlobKey blobKey = blobs.get("myFile");

Upvotes: 1

Related Questions