我有一个 SPI Google 端点,我找不到任何关于使用哪个注释来接受 MultipartFile 文件
的示例?
@ApiMethod(name = "saveNewBill" ,
httpMethod = ApiMethod.HttpMethod.POST)
public Bill saveBillImage( @Named("content") MultipartContent f ){
Bill bill = new Bill();
return bill;
}
最佳答案
编辑:我刚刚注意到,在阅读 Blobstore 相关的其他一些内容时,Google 现在建议使用 Google Cloud Storage 而不是 Blobstore 来提供媒体服务。
由于您在 App Engine 上使用端点,因此您应该使用 BlobStore
。
这是在 App Engine 上处理图像上传、存储和检索的首选方式。
BlobstoreService
中的以下函数将生成一个上传 URL,然后您可以使用标准多部分请求将图像上传到该 URL,并在名为 file
的参数中传递图像数据>.
BlobstoreServiceFactory.getBlobstoreService().createUploadUrl("/[servlet name goes here]");
您需要提供 Servlet 的名称,上传完成后请求将重定向到该 Servlet。此 Servlet 可以访问新创建的 Blobstore 项并用它执行一些有意义的操作 - 例如获取图像的服务 URL 并将其返回给客户端。
下面是执行此操作的 Servlet 片段:
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
BlobKey blobKey = blobs.get(0);
ImagesService imagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions servingOptions = ServingUrlOptions.Builder.withBlobKey(blobKey);
servingOptions.secureUrl(true);
String servingUrl = imagesService.getServingUrl(servingOptions);
res.setStatus(HttpServletResponse.SC_OK);
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.print(servingUrl);
out.flush();
out.close();
}
您还可以将一些您自己的查询字符串参数添加到上传 URL 中,这些参数可以在 Servlet 中读取。这对于将 BlobKey 附加到特定实体等事情非常有用。
要更深入地了解 Blobstore,我推荐 Romin Irish 的 App Engine 教程中的以下文章:https://rominirani.com/episode-13-using-the-blobstore-java-api-56423cf6a1b#.6t95vziul
关于java - 我应该使用哪个注释在 GAE 端点上上传图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38294221/