[Java] Dropbox API(Upload, Download, List)

Dropbox API 사용법

iOS Ad-hoc 버전 릴리즈 및 팀의 파일 공유를 위해 Dropbox를 사용하는데 이번에 CI(continuous integration)를 하려고 마음을 먹어서 단계별 기능을 확인하고 있다.

Dropbox API 사용법

https://www.dropbox.com/developers-v1/core/start/java 링크에 상세하게 설명되어 있다.

위의 단계에서 주의해야 할 사항은 AccessToken을 얻는 부분이다. 예제대로 따라 하면 AccessToken을 매번 새로 생성하기 때문에 재사용이 안된다.

그렇기 때문에 App Home – Settings – Generated access token 을 이용하여 AccessToken을 생성한다.


import com.dropbox.core.DbxClient;
import com.dropbox.core.DbxEntry;
import com.dropbox.core.DbxException;
import com.dropbox.core.DbxWriteMode;

import java.io.*;

public class DropboxAPI {   

    public DbxEntry.File uploadFile(DbxClient client, String fileName, long fileLength, byte[] fileBytes) throws IOException, DbxException {
        ByteArrayInputStream bais = new ByteArrayInputStream(fileBytes);
        DbxEntry.File uploadedFile = client.uploadFile("/CoozplzFile/" + fileName, DbxWriteMode.add(), fileLength, bais);
        return uploadedFile;

    public DbxEntry.WithChildren listAllFiles(DbxClient client) throws DbxException {
        DbxEntry.WithChildren children = client.getMetadataWithChildren("/CoozplzFile");
        return children;

    public DbxEntry.File downloadFile(DbxClient client, String sourceFile, File downloadedPath) throws IOException, DbxException {
        FileOutputStream fos = new FileOutputStream(downloadedPath);
        DbxEntry.File downloadedFile = client.getFile("/CoozplzFile/"+ sourceFile, null, fos);
        return downloadedFile;


import com.dropbox.core.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.List;
import java.util.Locale;

public class DropboxAPITest {

    public static String ACCESS_TOKEN = "ACCESS_TOKEN";

    DbxClient client;

    public void setup() {
        DbxRequestConfig config = new DbxRequestConfig("Sample/1.0", Locale.getDefault().toString());
        client = new DbxClient(config, ACCESS_TOKEN);

    public void testListFiles() throws DbxException {

        DropboxAPI dropboxAPI = new DropboxAPI();
        DbxEntry.WithChildren children = dropboxAPI.listAllFiles(client);
        List<DbxEntry> list = children.children;

    public void testUpload() throws IOException, DbxException {
        DropboxAPI dropboxAPI = new DropboxAPI();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        InputStream in = DropboxAPITest.class.getResourceAsStream("101-N101_c.ai");

        byte[] buf = new byte[1024];
        while (true) {
            int readSize = in.read(buf);
            if (readSize < 0) {
            baos.write(buf, 0, readSize);
        DbxEntry.File uploadedFile = dropboxAPI.uploadFile(client, "101-N101_c.ai", baos.size(), baos.toByteArray());
        Assert.assertEquals(baos.size(), uploadedFile.numBytes);
        System.out.println("Numbers of bytes = " + uploadedFile.numBytes);

    public void testDownload() throws IOException, DbxException {
        DropboxAPI dropboxAPI = new DropboxAPI();

        DbxEntry.File file = dropboxAPI.downloadFile(client, "101-N101_c.ai", new File("D:/101-N101_c.ai"));

        String url = client.createShareableUrl(file.path);




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s