简体   繁体   English

如何从InputStream存储Blob

[英]How do I store a Blob from InputStream

I am using Jersey and I can upload a file, I can also write dummy blobs to the datastore. 我正在使用Jersey,我可以上传文件,我也可以写虚拟blob到数据存储区。 but I am stuck at writing the file that I uploaded to the datastore. 但我坚持写上传到数据存储区的文件。

@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));
    }
}

If I understand your question correctly, you test code is working: 如果我正确理解您的问题,那么测试代码是有效的:

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

But it is not working when you try to reading you InputStream you just uploaded. 但是,当您尝试读取刚刚上传的InputStream时,它无效。

Few tracks to follow: 很少有曲目要遵循:

  1. Your function use @FormDataParam("file") for two parameters with different type (stream and disposition) . 您的函数使用@FormDataParam("file")来表示具有不同类型(流和处置)的两个参数。 Doesn't look right. 看起来不对。

  2. Are you sure the uploaded file is not null? 你确定上传的文件不是空的吗?

  3. Try to read the file directly form the disk without uploading. 尝试直接从磁盘读取文件而不上传。 It might help you figuring out if the problem come from the upload or not. 它可能会帮助您确定问题是否来自上传。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM