java - 如何存储来自 InputStream 的 Blob

标签 java google-app-engine jersey google-cloud-datastore

我正在使用 Jersey,我可以上传文件,我还可以将虚拟 blob 写入数据存储区。但我一直坚持编写上传到数据存储的文件。

@Path("/blob/")
public class UploadFileService {

    @POST
    @Path("/upload/")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(@FormDataParam("file") InputStream stream, @FormDataParam("file") FormDataContentDisposition disposition) throws IOException {

        storeBlob();

      return Response.ok().build();
    }

    public void storeBlob() throws IOException{


        // Get a file service
          FileService fileService = FileServiceFactory.getFileService();

          // Create a new Blob file with mime-type "text/plain"
          AppEngineFile file = fileService.createNewBlobFile("text/plain");

          // Open a channel to write to it
          boolean lock = false;
          FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);

          // Different standard Java ways of writing to the channel
          // are possible. Here we use a PrintWriter:
          PrintWriter out = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
          out.println("The woods are lovely dark and deep.");
          out.println("But I have promises to keep.");

          // Close without finalizing and save the file path for writing later
          out.close();
          String path = file.getFullPath();

          // Write more to the file in a separate request:
          file = new AppEngineFile(path);

          // This time lock because we intend to finalize
          lock = true;
          writeChannel = fileService.openWriteChannel(file, lock);

          // This time we write to the channel directly
          writeChannel.write(ByteBuffer.wrap
                    ("And miles to go before I sleep.".getBytes()));

          // Now finalize
          writeChannel.closeFinally();

          // Later, read from the file using the Files API
          lock = false; // Let other people read at the same time
          FileReadChannel readChannel = fileService.openReadChannel(file, false);

          // Again, different standard Java ways of reading from the channel.
          BufferedReader reader =
                  new BufferedReader(Channels.newReader(readChannel, "UTF8"));
               String line = reader.readLine();
          // line = "The woods are lovely dark and deep."

          readChannel.close();

          // Now read from the file using the Blobstore API
          BlobKey blobKey = fileService.getBlobKey(file);
          BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
          String segment = new String(blobStoreService.fetchData(blobKey, 30, 40));
    }
}

最佳答案

如果我正确理解你的问题,那么你的测试代码正在工作:

// This time we write to the channel directly
writeChannel.write(ByteBuffer.wrap, "And miles to go before I sleep.".getBytes());

但是当您尝试读取刚刚上传的 InputStream 时,它不起作用。

几乎没有什么可遵循的:

  1. 您的函数使用 @FormDataParam("file") 作为两个不同类型的参数(流和处置)。看起来不太对劲。

  2. 您确定上传的文件不为空吗?

  3. 尝试直接从磁盘读取文件而不上传。它可能会帮助您确定问题是否来自上传。

关于java - 如何存储来自 InputStream 的 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17475514/

相关文章:

java - 为什么 String、StringBuffer 和 StringBuilder 类使用字节数组而不是字符数组来存储字符串的字符?

java - 如何以编程方式将标题栏颜色更改为十六进制值?

python - 应用引擎 : restore original builtin open/file function

java - 无法使用 servlet 3 可插入性功能将 Jersey 2.16 集成到 Tomcat 8.0.12

java - 为什么 Jersey 会导致单例类无法工作?

java - mod_jk 或 mod_proxy_ajp 用于集群 tomcat 和 ssl

java - JMenuItem 在左侧显示复选框,如何禁用它?

java - 使用 Maven EAR 打包的 GWT 应用程序运行 SuperDevMode

java - Noclassdeffounderror - 找不到 v7 支持(android studio)

java - 将 JAXB 声明为依赖项。为什么?