我正在寻找上传和存储图像(文件)到GAE(java)最简单的方法。 谷歌搜索小时,没有任何简单明了的结果...:(如何上传和存储图像与谷歌应用程序引擎(Java)
但我仍然不知道如何存储的图像,以及如何检索它... 我。寻找简单的servlet exmample ...
我正在寻找上传和存储图像(文件)到GAE(java)最简单的方法。 谷歌搜索小时,没有任何简单明了的结果...:(如何上传和存储图像与谷歌应用程序引擎(Java)
但我仍然不知道如何存储的图像,以及如何检索它... 我。寻找简单的servlet exmample ...
链接您提供"How do I handle file uploads to my app?"介绍了如何上传图片。
主办的图像,你需要使用Datastore service来与其他数据一起存储和提供图片。
这里是一个示例代码。它的意思是作为一个草图,你如何拥有自己的实体(例如企业,用户等)拥有一个图像领域。我忽略了所有错误处理和恢复来简化代码。
用图像声明你的实体。你可以想象有其他领域,例如标签,位置等
@Entity
public class MyImage {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String name;
@Persistent
Blob image;
public MyImage() { }
public MyImage(String name, Blob image) {
this.name = name;
this.image = image;
}
// JPA getters and setters and empty contructor
// ...
public Blob getImage() { return image; }
public void setImage(Blob image) { this.image = image; }
}
然后,当你开始接受影像(注意,其中具有相同名称的图像已经在除了典型的文件上传失败上传的情况下)。 ServletFileUpload
和IOUtils
是Apache Commons库的一部分。
// Your upload handle would look like
public void doPost(HttpServletRequest req, HttpServletResponse res) {
// Get the image representation
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(req);
FileItemStream imageItem = iter.next();
InputStream imgStream = imageItem.openStream();
// construct our entity objects
Blob imageBlob = new Blob(IOUtils.toByteArray(imgStream));
MyImage myImage = new MyImage(imageItem.getName(), imageBlob);
// persist image
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.makePersistent(myImage);
pm.close();
// respond to query
res.setContentType("text/plain");
res.getOutputStream().write("OK!".getBytes());
}
最后当你想成为一个像它的名字:
Blob imageFor(String name, HttpServletResponse res) {
// find desired image
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery("select from MyImage " +
"where name = nameParam " +
"parameters String nameParam");
List<MyImage> results = (List<MyImage>)query.execute(name);
Blob image = results.iterator().next().getImage();
// serve the first image
res.setContentType("image/jpeg");
res.getOutputStream().write(image.getBytes());
}
的Blob存储API允许应用程序服务数据对象,称为blob,这比数据存储服务中的对象所允许的大小要大得多。 Blob对于提供大型文件(如视频或图像文件)以及允许用户上传大型数据文件非常有用。通过HTTP请求上传文件来创建Blob。通常,您的应用程序将通过向用户呈现带有文件上传字段的表单来完成此操作。提交表单时,Blobstore会根据文件内容创建一个blob,并返回一个不透明的引用,称为blob密钥,稍后您可以使用该引用来提供blob。应用程序可以响应用户请求提供完整的blob值,也可以直接使用类流式文件接口读取值...
最简单的方式使用Google App Engine Blob Store服务URL(你节省实例时间)
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.ServingUrlOptions;
...
// your data in byte[] format
byte[] data = image.getData();
/**
* MIME Type for
* JPG use "image/jpeg" for PNG use "image/png"
* PDF use "application/pdf"
* see more: https://en.wikipedia.org/wiki/Internet_media_type
*/
String mimeType = "image/jpeg";
// save data to Google App Engine Blobstore
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mimeType);
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
writeChannel.write(java.nio.ByteBuffer.wrap(data));
writeChannel.closeFinally();
// your blobKey to your data in Google App Engine BlobStore
BlobKey blobKey = fileService.getBlobKey(file);
// THANKS TO BLOBKEY YOU CAN GET FOR EXAMPLE SERVING URL FOR IMAGES
// Get the image serving URL (in https:// format)
String imageUrl =
ImagesServiceFactory.getImagesService().getServingUrl(
ServingUrlOptions.Builder.withBlobKey(blobKey
).secureUrl(true));
警告!上面的代码已被弃用。 – 2014-09-08 13:17:02
以上代码的任何替代方式已弃用? – 2016-03-24 04:29:40
我相信服务图像的新方式可能是这样的云存储: https://cloud.google.com/python/getting-started/using-cloud-storage – 2017-09-11 20:12:08
非常感谢你的这个清晰和易于理解的答案! :) – OXMO456 2009-10-03 17:29:56
太棒了,谢谢。你能告诉我为什么字节数组不会给出正确的输出,虽然它被存储等,我刚才提出了这个问题http://stackoverflow.com/questions/2018597/cannot-fetch-image-data-in- gwt-google-datastore-image-is-stre-out-out – dhaval 2010-01-29 18:44:22
优秀的答案! – 2010-08-04 05:05:02