【问题标题】:Writing files to the Blobstore AppEngine and serving to client将文件写入 Blobstore AppEngine 并提供给客户端
【发布时间】:2011-12-30 04:45:50
【问题描述】:

目标: 客户端将字符串输入发送到服务器(App Engine)。服务器修改输入,使用输出创建一个文件并将其提供给客户端。 GWT 项目。

这是我的代码方案(服务器端和客户端),但我不知道如何将文件提供给客户端。每当我尝试在客户端输入任何 BlobStore 导入时,都会在运行时出错(但不是在构建或编译时)。

将文件写入 Blobstore 被标记为实验性 (http://code.google.com/appengine/docs/java/blobstore/overview.html#Writing_Files_to_the_Blobstore)。也许它还没有工作?你能帮我解决这个问题吗?即使不使用 Blob,只要满足上述目标即可。谢谢。

ProjectServiceImpl.java

public class ProjectServiceImpl extends RemoteServiceServlet implements ProjectService 
{
    public String project(String input) throws IllegalArgumentException 
    {
        String output = doSomethingWith(input);
        FileService fileService = FileServiceFactory.getFileService();
        AppEngineFile file = fileService.createNewBlobFile("text/plain");
        boolean lock = true;
        FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
        writeChannel.write(ByteBuffer.wrap("Hello world!".getBytes()));
        writeChannel.closeFinally();
        BlobKey blobKey = fileService.getBlobKey(file);
        BlobstoreService blobService = BlobstoreServiceFactory.getBlobstoreService();
    }
}

ProjectService.java

public interface ProjectService extends RemoteService {
    String project(String name) throws IllegalArgumentException;
}

ProjectServiceAsync.java

public interface ProjectServiceAsync {
    void project(String input, AsyncCallback<String> callback)
            throws IllegalArgumentException;
}

MyProject.java:客户端

[...]
projectService.project(originalString, new AsyncCallback<String>() {
    [...]
    public void onSuccess(final String result) 
    {
        BlobstoreService blobService = BlobstoreServiceFactory.getBlobstoreService();
    }
});

【问题讨论】:

    标签: java google-app-engine gwt blob blobstore


    【解决方案1】:

    您不能在客户端使用 App Engine API(包括 blobstore API)。 API 仅适用于 App Engine 应用,而不适用于用户 Javascript。要提供 blob,请按照说明进行操作 here

    【讨论】:

    • 谢谢尼克!因此,只需在您提供的链接中添加 Serve.java 并将正确的 blobKey 传递给该类就足够了?不需要在 MyProject.java 中添加任何 Blob 代码吗?我是否需要我的服务器端类来扩展 HttpServlet 而不是 RemoteService,如链接中的示例所示?谢谢!
    • @user411103 编写一个类似演示中的servlet;它的工作方式取决于您希望如何为您的 blob 服务。您的 servlet 确实需要扩展 HttpServlet。
    猜你喜欢
    • 2012-01-17
    • 2012-07-23
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2010-11-07
    • 2011-05-29
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多