如何从InputStream存储Blob

3

我正在使用Jersey,可以上传文件,也可以将虚拟数据块写入数据存储区。但是,我卡在了将上传的文件写入数据存储区的问题上。

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

你能否详细说明一下你写的代码中哪些部分出了问题? - AlikElzin-kilaka
1个回答

0

如果我理解你的问题正确,你的测试代码是正常工作的:

// 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. 尝试直接从磁盘上读取文件而不进行上传。这可能有助于您确定问题是否来自上传。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接