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

会有一股神秘感。 提交于 2020-01-07 02:37:08

问题


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.


回答1:


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.




回答2:


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");


来源:https://stackoverflow.com/questions/14118571/how-to-get-uploaded-file-as-blobs-in-servlet-using-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!