如何从appengine数据存储实体对象中逐字节读取

发布于 2024-10-30 12:51:03 字数 1223 浏览 3 评论 0原文

简而言之,由于 GAE 无法写入文件系统,因此我决定将数据保存到数据存储中(使用 JDO)。现在,我想逐字节检索数据并将其作为输入流传递给客户端。 gwtupload 库(http://code.google.com/p/gwtupload/)(见下文)中的代码会在 GAE 上中断,因为它会写入系统文件系统。我希望能够提供 GAE 移植解决方案。

public static void copyFromInputStreamToOutputStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[100000];
    while (true) {
      synchronized (buffer) {
        int amountRead = in.read(buffer);
        if (amountRead == -1) {
          break;
        }
        out.write(buffer, 0, amountRead);
      }
    }
    in.close();
    out.flush();
    out.close();
  }

我尝试过的一种解决方法(没有成功)是从数据存储中检索数据作为资源,如下所示:

InputStream resourceAsStream = null;
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        Query q = pm.newQuery(ImageFile.class);
        lf  = q.execute();
        resourceAsStream = getServletContext().getResourceAsStream((String) pm.getObjectById(lf));
    } finally {
      pm.close();
    }
    if (lf != null) {
      response.setContentType(receivedContentTypes.get(fieldName));
      copyFromInputStreamToOutputStream(resourceAsStream, response.getOutputStream());

    } 

我欢迎您的建议。

问候

In a nutshell, since GAE cannot write to a filesystem, I have decided to persist my data into the datastore (using JDO). Now, I will like to retrieve the data byte by byte and pass it to the client as an input stream. There's code from the gwtupload library(http://code.google.com/p/gwtupload/) (see below) which breaks on GAE because it writes to the system filesystem. I'll like to be able to provide a GAE ported solution.

public static void copyFromInputStreamToOutputStream(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[100000];
    while (true) {
      synchronized (buffer) {
        int amountRead = in.read(buffer);
        if (amountRead == -1) {
          break;
        }
        out.write(buffer, 0, amountRead);
      }
    }
    in.close();
    out.flush();
    out.close();
  }

One work around I have tried (didn't work) is to retrieve the data from the datastore as a resource like this:

InputStream resourceAsStream = null;
    PersistenceManager pm = PMF.get().getPersistenceManager();
    try {
        Query q = pm.newQuery(ImageFile.class);
        lf  = q.execute();
        resourceAsStream = getServletContext().getResourceAsStream((String) pm.getObjectById(lf));
    } finally {
      pm.close();
    }
    if (lf != null) {
      response.setContentType(receivedContentTypes.get(fieldName));
      copyFromInputStreamToOutputStream(resourceAsStream, response.getOutputStream());

    } 

I welcome your suggestions.

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2024-11-06 12:51:03

将数据存储在字节数组中,并使用 ByteArrayInputStream 或 ByteArrayOutputStream 将其传递给需要流的库。

但是,如果“客户端”指的是“HTTP 客户端”或浏览器,则没有理由这样做 - 只需在您的端处理常规字节数组并将它们发送给用户或从用户发送它们,就像处理任何其他数据一样。搞乱这样的流的唯一原因是如果你有一些需要它们的库。

Store data in a byte array, and use a ByteArrayInputStream or ByteArrayOutputStream to pass it to libraries that expect streams.

If by 'client' you mean 'HTTP client' or browser, though, there's no reason to do this - just deal with regular byte arrays on your end and send them to/from the user as you would any other data. The only reason to mess around with streams like this is if you have some library that expects them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文