首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将文件从DropBox下载到本地计算机

将文件从DropBox下载到本地计算机
EN

Stack Overflow用户
提问于 2014-04-03 19:58:53
回答 1查看 3.9K关注 0票数 5

我想使用java dropbox API从我的DropBox帐户下载一个文件。我已尝试使用此代码,但这是显示文件和文件夹的列表,而我想下载文件到我的系统是如何可能的

这是我的代码:

代码语言:javascript
复制
Scanner tokenScanner = new Scanner(tokensFile);       
       String ACCESS_TOKEN_KEY = tokenScanner.nextLine();    // Read key
       String ACCESS_TOKEN_SECRET = tokenScanner.nextLine(); // Read secret
       tokenScanner.close(); //Close Scanner
       //Re-auth
       AccessTokenPair reAuthTokens = new AccessTokenPair(ACCESS_TOKEN_KEY,ACCESS_TOKEN_SECRET);
       mDBApi.getSession().setAccessTokenPair(reAuthTokens);
       Entry entries = mDBApi.metadata("/", 20, null, true, null);
       for (Entry e: entries.contents) {
        if(!e.isDeleted){
         if(e.isDir){
          System.out.println("Folder ---> " + e.fileName() );
         } else {
          //  this will download the root level files.
          System.out.println("File ---->" + e.fileName());
          DropboxInputStream inputStream = mDBApi.getFileStream(e.path,null);
          OutputStream out=new FileOutputStream(e.fileName());
          byte buf[]=new byte[1024];
          int len;
          while((len=inputStream.read(buf))>0)
           out.write(buf,0,len);
               out.close();
               inputStream.close();
               System.out.println("File is created....");
EN

回答 1

Stack Overflow用户

发布于 2016-07-29 22:11:14

这是一个下载dropbox文件的基本示例。它不包括文件下载的进度,这只是为了直接下载文件。

此示例使用使用DbxClientV2的dropbox API v2

代码语言:javascript
复制
        try
            {
                //output file for download --> storage location on local system to download file
                OutputStream downloadFile = new FileOutputStream("C:\\.....");
                try
                {
                FileMetadata metadata = client.files().downloadBuilder("/root or foldername here/Koala.jpg")
                        .download(downloadFile);
                }
                finally
                {
                    downloadFile.close();
                }
            }
            //exception handled
            catch (DbxException e)
            {
                //error downloading file
                JOptionPane.showMessageDialog(null, "Unable to download file to local system\n Error: " + e);
            }
            catch (IOException e)
            {
                //error downloading file
                JOptionPane.showMessageDialog(null, "Unable to download file to local system\n Error: " + e);
            }

希望这会有所帮助,并使用此示例并编辑它,使其按照您希望的方式工作。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22837030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档