【问题标题】:java.nio.file.NoSuchFileException - Download files from GCSjava.nio.file.NoSuchFileException - 从 GCS 下载文件
【发布时间】:2019-03-24 16:21:33
【问题描述】:

我正在尝试从 Google Cloud Storage 下载文件。我正在使用 Google 的 Github 代码 here on line 1042。我相信我的错误在于文件路径。路径变量用于说明文件下载到的位置及其新名称,对吗?请注意,我正在使用 JSP 按钮来启动该过程。为了让自己更容易解决,我用字符串而不是 HttpServletRequest 替换了字符串和路径变量。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // [START storage_download_file]
    // The name of the bucket to access
    String bucketName = "media";

    // The name of the remote file to download
    String srcFilename = "File.rtf";

    // The path to which the file should be downloaded
    Path destFilePath = Paths.get("/Volumes/Macintosh HD/Users/ab/Desktop/File.rtf");

    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();

    // Get specific file from specified bucket
    Blob blob = storage.get(BlobId.of(bucketName, srcFilename));

    // Download file to specified path
    blob.downloadTo(destFilePath);
    // [END storage_download_file]
}

原因:java.nio.file.NoSuchFileException: /Volumes/Macintosh HD/Users/ab/Desktop/File.rtf

【问题讨论】:

  • 此路径是否存在于您运行代码的机器上?检查路径和文件
  • 代码在 GCP 的 App Engine 上运行。我对 Path 变量感到困惑。我了解该代码会将 File.rtf 从 GCS 下载到我的计算机。它会将下载的文件放置在具有新名称的指定路径中,在本例中为相同名称,该名称在路径末尾指定。路径最终为 /Users/ab/Desktop/File.rtf。不包括 /Volumes/Macintosh HD。我得到了同样的错误。
  • 如果该代码在 Google App Engine 上运行,它将尝试在 Google App Engine 机器上写入,而不是在您的计算机上。要将其下载到您的计算机,您必须将下载的字节写入 HTTP 响应输出流。

标签: java google-cloud-platform nosuchfileexception


【解决方案1】:

GitHub in the Google APIs 上找到的代码。我缺少的部分和概念是

OutputStream outStream = response.getOutputStream();

这会将数据发送回客户端的浏览器。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   

    /**** Define variables ****/
    Storage storage = StorageOptions.getDefaultInstance().getService();
    String bucketName = "bucketName";
    String objectName = "objectName";

    /**** Setting The Content Attributes For The Response Object ****/
    String mimeType = "application/octet-stream";
    response.setContentType(mimeType);

    /**** Setting The Headers For The Response Object ****/
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", objectName);
    response.setHeader(headerKey, headerValue);

    /**** Get the Output Stream of the Response Object****/
    OutputStream outStream = response.getOutputStream();

    /**** Call download method ****/
    run(storage, bucketName, objectName, outStream);
}

private void run(Storage storage, String bucketName, String objectName, OutputStream outStream)throws IOException {
    /**** Getting the blob ****/
    BlobId blobId = BlobId.of(bucketName, objectName);
    Blob blob = storage.get(blobId);

    /**** Writing the content that will be sent in the response ****/
    byte[] content = blob.getContent();
    outStream.write(content);
    outStream.close();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-16
    • 2020-12-22
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    相关资源
    最近更新 更多