【问题标题】:Google Cloud Storage createOrReplace file is broken (different size, ...)Google Cloud Storage createOrReplace 文件已损坏(大小不同,...)
【发布时间】:2013-08-15 09:06:12
【问题描述】:

我尝试将流(restlet memoryfile)上传到 gcs。但是该文件有另一个文件大小并且略有不同,因此该文件被标记为“损坏”。

我尝试的是本地和谷歌应用引擎。在调试这部分时,流的大小看起来不错InputStream inputStream = item.getInputStream();

但是商店里的结果不是那么大。开头有4位:¬í[NUL][ENQ]

他们来自哪里?

List<FileItem> items;
try {
    MemoryFileItemFactory factory = new MemoryFileItemFactory();
    RestletFileUpload restletFileUpload = new RestletFileUpload(factory);
    items = restletFileUpload.parseRequest(req);
    //items = restletFileUpload.parseRepresentation(entity);
    for (FileItem item : items) {
       if (!item.isFormField()) {
           MediaType type = MediaType.valueOf(item.getContentType());
            GcsFileOptions options = new GcsFileOptions.Builder().mimeType(type.getName()).acl("public-read").build();
            GcsOutputChannel outputChannel = gcsService.createOrReplace(fileName, options);
            ObjectOutputStream oout = new ObjectOutputStream(Channels.newOutputStream(outputChannel)); 

           InputStream inputStream = item.getInputStream();
           copy(inputStream, oout);
           //oout.close();
       }
   }
} catch (FileUploadException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

private void copy(InputStream input, OutputStream output) throws IOException {
    try {
      byte[] buffer = new byte[BUFFER_SIZE];
      int bytesRead = input.read(buffer);
      while (bytesRead != -1) {
        output.write(buffer, 0, bytesRead);
        bytesRead = input.read(buffer);
      }
    } finally {
      input.close();
      output.close();
    }
}

private final GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
  .initialRetryDelayMillis(10)
  .retryMaxAttempts(10)
  .totalRetryPeriodMillis(15000)
  .build());

【问题讨论】:

    标签: java image google-app-engine rest google-cloud-storage


    【解决方案1】:

    从复制函数中删除 finally close 语句并改为关闭 GcsOutputChannel。此外,您不需要这样做:ObjectOutputStream oout = new ObjectOutputStream(Channels.newOutputStream(outputChannel)); 也许这增加了额外的位

    类似的东西:

    GcsOutputChannel outputChannel = gcsService.createOrReplace(fileName, options);
    InputStream inputStream = item.getInputStream();
    
    try {
        copy(inputStream, Channels.newOutputStream(outputChannel));
    } finally {
        outputChannel.close();
        inputStream.close();
    }
    
    private void copy(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = input.read(buffer);
        while (bytesRead != -1) {
            output.write(buffer, 0, bytesRead);
            bytesRead = input.read(buffer);
        }
    }
    

    【讨论】:

    • 谢谢。有了这个 sn-p,我可以上传正确的文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2019-07-08
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2020-06-18
    相关资源
    最近更新 更多