能够获取 Box 文件夹项目,但无法使用 API 从 box 下载文件

Able to fetch Box folder items but could not download the file from box using API

我正在尝试通过传递文件 ID 从 Box 下载文件,但出现以下错误。尽管我能够从 Box 中获取文件夹项目并能够成功获取文件夹内的所有文件名,这表明我的应用程序和 Box api 之间存在网络连接。同样的代码在本地机器上也能按预期工作,但在代码迁移到云端后却无法工作。

下载 API 是否使用与获取文件夹项目不同的端口? Box API 文档告诉所有 API 使用 https.

INFO   | jvm 1    | main    | 2019/08/08 11:26:32.626 | com.box.sdk.BoxAPIException: Couldn't connect to the Box API due to a network error.
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.626 |         at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:551)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.626 |         at com.box.sdk.BoxAPIRequest.handleRedirect(BoxAPIRequest.java:615)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.626 |         at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:571)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:354)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:329)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.box.sdk.BoxFile.download(BoxFile.java:295)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.box.sdk.BoxFile.download(BoxFile.java:283)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.ge.hc.integration.service.impl.BoxIntegrationSerImpl.getBoxFileByFileID(BoxIntegrationSerImpl.java:188)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 | Caused by: java.net.ConnectException: Connection timed out (Connection timed out) (local port 38216 to address 0.0.0.0, remote port 443 to address 107.152.27.200)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.PlainSocketImpl.socketConnect(Native Method)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.Socket.connect(Socket.java:857)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:666)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at sun.net.www.http.HttpClient.openServer(HttpClient.java:569)`

文件下载的代码片段(在 boxFile.download(stream) 处抛出错误的代码片段)--

String fileDownloadLocation = siteConfigService.getProperty(FILEDOWNLOADLOCATION);
File file = null;
BoxAPIConnection boxConnection = new BoxAPIConnection(accessToken);

    FileOutputStream stream;
    try 
    {
        BoxFile boxFile = new BoxFile(boxConnection, fileID);
        BoxFile.Info info = boxFile.getInfo();
        LOG.info("downloading file to -"+fileDownloadLocation+info.getName());
        stream = new FileOutputStream(fileDownloadLocation+info.getName());
        //LOG.info(stream);
        boxFile.download(stream);
        stream.close();
        file = new File(fileDownloadLocation+info.getName());
    } 

获取文件夹项目的代码片段(正在工作并打印文件名的那个)--

public List<Info> getFolderItems(String accessToken, String folderID) {
    List<Info> fileList = new ArrayList<Info>();
    BoxAPIConnection boxConnection = new BoxAPIConnection(accessToken);
    BoxFolder folder = new BoxFolder(boxConnection, folderID);

    for (BoxItem.Info itemInfo : folder) {
        if (itemInfo instanceof BoxFile.Info) {
            BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
            fileList.add(fileInfo);
            LOG.info("***FILE NAME-"+fileInfo.getName());
        } else if (itemInfo instanceof BoxFolder.Info) {
            BoxFolder.Info folderInfo = (BoxFolder.Info) itemInfo;
        }
    }
    return fileList;
}

Box 在与 API (api.box.com) 不同的域 (dl.boxcloud.com) 上托管文件内容。当您 download a file via the API 时,API returns 在 boxcloud.com 域上临时 URL。 Java SDK 自动遵循此重定向来获取文件内容。所有请求都通过端口 443 (HTTPS) 发出。

这可能只是暂时性错误。如果它仍然存在,您可能会检查您的云服务是否配置了任何防火墙规则。以下是此次操作涉及的域名和IP地址:

api.box.com: 107.152.26.197, 107.152.27.197

dl.boxcloud.com: 107.152.26.200, 107.152.27.200

注意:dl.boxcloud.com IP 出现在您的错误日志中,这表明 SDK 按照预期进行了重定向。