如何在Java中将图像上传到DropBox

本教程介绍了如何将图像上传到放置框并获取上传图像的公共URL。

首先,我们必须使用应用程序控制台创建一个DropBox API应用程序 。 创建应用程序后,您可以在应用程序属性中获取应用程序密钥和秘密密钥。

现在在您的pom文件中添加以下依赖项。

<dependency>
 <groupId>com.dropbox.core</groupId>
 <artifactId>dropbox-core-sdk</artifactId>
 <version>1.7.7</version>
</dependency>

现在,此Java程序将完成其余工作。 替换程序中的应用程序密钥和密钥。 从命令行运行此Java程序,它将要求输入代码,您将通过遵循控制台上打印的URL来获取代码。

为了获取公共URL,我们只需要使用dropboxClient类的createShareableUrl。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;

import com.dropbox.core.DbxAppInfo;
import com.dropbox.core.DbxAuthFinish;
import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxRequestConfig;
import com.dropbox.core.DbxWebAuthNoRedirect;
import com.dropbox.core.DbxWriteMode;


public class UploadImages
{
 public static void main(String[] args) throws IOException, DbxException
 {
  final String DROP_BOX_APP_KEY = "APPKEY";
        final String DROP_BOX_APP_SECRET = "SECRETKEY";
        
        String rootDir = "C:\\Users\\Downloads\\";

        DbxAppInfo dbxAppInfo = new DbxAppInfo(DROP_BOX_APP_KEY, DROP_BOX_APP_SECRET);

        DbxRequestConfig reqConfig = new DbxRequestConfig("javarootsDropbox/1.0",
            Locale.getDefault().toString());
        DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(reqConfig, dbxAppInfo);

        
        String authorizeUrl = webAuth.start();
        System.out.println("1. Go to this URL : " + authorizeUrl);
        System.out.println("2. Click \"Allow\" (you might have to log in first)");
        System.out.println("3. Copy the authorization code and paste here ");
        String code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();

        
        
        DbxAuthFinish authFinish = webAuth.finish(code);
        String accessToken = authFinish.accessToken;

        DbxClient client = new DbxClient(reqConfig, accessToken);

        System.out.println("account name is : " + client.getAccountInfo().displayName);

        
        
        File inputFile = new File(rootDir +"images\\"+ "javaroots.jpg");
        FileInputStream inputStream = new FileInputStream(inputFile);
        try {
         
            DbxEntry.File uploadedFile = client.uploadFile("/javaroots.jpg",
                DbxWriteMode.add(), inputFile.length(), inputStream);
            String sharedUrl = client.createShareableUrl("/javaroots.jpg");
            System.out.println("Uploaded: " + uploadedFile.toString() + " URL " + sharedUrl);
        } finally {
            inputStream.close();
        }
 }
}

请参考此官方投递箱链接。

  • 您可以从此链接下载完整的项目。 发表评论和建议!

翻译自: https://www.javacodegeeks.com/2014/11/how-to-upload-images-to-dropbox-in-java.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值