Stripes + GAE如何上传文件

时间:2014-01-10 11:37:48

标签: java google-app-engine file-upload google-cloud-storage stripes

我想在我的应用中实现文件上传。条纹+ gae的问题是条纹在处理之前在本地存储上传的文件,并且gae不允许这样做。

我决定使用GCS和blobstore API上传文件,但遇到了问题。

我的网页代码如下:

<form action="${actionBean.servletUploadUrl}" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile">
        <input type="submit" value="Submit">
</form>

我尝试以两种方式实现我的服务器端代码:

1 getServletUploadUrl()就像

public String getServletUploadUrl() {
   String bucket = "my-bucket";
   BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
   UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName(bucket);
   return blobstoreService.createUploadUrl("/MyActionBean.action?saveDocument=", uploadOptions);
}

通过这种方式,我得到一个例外:Could not construct a MultipartWrapper for the current request.

2 (差不多但尝试使用servlet)

getServletUploadUrl()就像

public String getServletUploadUrl() {
    String bucket = "my-bucket";
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName(bucket);
    return blobstoreService.createUploadUrl("/docUpload", uploadOptions);
}

其中/docUpload在web.xml中映射到DocumentUploadServlet,其中我实现了doPost方法。

我到了那个servlet但是之后我想在我的action bean中使用saveDocument()方法。我添加了行

req.getRequestDispatcher("/MyActionBean.action?saveDocument=").forward(req, resp);

再次以我的第一种方式获得例外:Could not construct a MultipartWrapper for the current request.

似乎问题是Stripes无法包装请求,因为两个异常都是这样的:

net.sourceforge.stripes.exception.StripesRuntimeException: Could not construct a MultipartWrapper for the current request.
at net.sourceforge.stripes.controller.multipart.DefaultMultipartWrapperFactory.wrap(DefaultMultipartWrapperFactory.java:158)
at net.sourceforge.stripes.controller.StripesRequestWrapper.constructMultipartWrapper(StripesRequestWrapper.java:126)
at net.sourceforge.stripes.controller.StripesRequestWrapper.<init>(StripesRequestWrapper.java:106)
at net.sourceforge.stripes.controller.StripesFilter.wrapRequest(StripesFilter.java:295)

可能有更好的文件上传方式。任何想法都表示赞赏!

0 个答案:

没有答案