如何使用Google App Engine在Servlet中将文件上传为Blob [英] how to get uploaded file as blobs in servlet using google app engine

查看:36
本文介绍了如何使用Google App Engine在Servlet中将文件上传为Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码将文件上传为blob,

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>

但以我为例,我正在使用 Apache通用文件上传器上传文件.因此,我的表单操作如下所示,

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

现在我的servlet中正在将文件作为InputStream获取.如果我想将文件转换为同一servlet文件中的blob,该如何转换.请给我一个主意.

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.

已更新

我尝试了将文件写入Blob存储区我的代码如下,

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

现在我可以在App Engine中上传文件,它存储Blob值,但我无法查看Blob值.当我使用<%= blobstoreService.createUploadUrl("/upload")%> 时,我的应用程序引擎向我显示查看Blob"选项,但现在不存在.列表以查看未显示的文件,并说"0"字节.我认为它只是存储Blob键,而不存储文件.请给我一个解决问题的主意.

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.

推荐答案

有两种方法可以做到这一点:

There are two ways to do this:

  1. 使用提供的Blobstore上传功能:您的客户端调用使用一次性上传网址(通过 blobstoreService.createUploadUrl("/upload")创建)的服务器.然后,您可以一次性将此URL与Apache Common File Uploader一起使用.

  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.

处理您代码中的上载:创建一个多部分处理程序,然后然后通过Files API通过程序将数据保存到blob存储.

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

请注意,选项2属于标准GAE 32Mb请求/响应大小限制.选项1.的文件上传限制为2Gb.

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

这篇关于如何使用Google App Engine在Servlet中将文件上传为Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆