leisenfels mit schrift und slogan 48

VFSLib Dropbox Example

 

Please read the following steps to use the VFSLib Java library. This example demonstrates the usage of the Dropbox © provider included in the VFSLib artifacts.

The Java example code is based on the original Apache Commons VFS 2.0 classes like DefaultFileSystemManager and thus should integrate seamlessly into existing implementations.
 

 


Step 1: Download VFSLib

First of all please download the current VFSLib source archive on your computer and extract.

In the vfslib-src-2.8.2\src\test\java\com\lf\vfslib\test\dropbox folder you can find the Dropbox example code to be executed directly in your favorite IDE like IntelliJ IDEA.

Please take a look at the README.md to get the Maven project up and running from pom.xml.

 


Step 2: Running The Dropbox Example

Please go create a Dropbox account and create an application via the Dropbox website, then grant your application the following permissions via the Dropbox App Console:

  • account_info.read
  • files.metadata.read
  • files.content.write
  • files.content.read

 Start the example main class(es) via your IDE:

  • ExampleDropbox.java            Based on VFS 2.0 standards only (recommended)
  • ExampleDropboxOptimized.java   Uses VFSLib specific optimizations

 

Hint

You will need to run the example in a graphical environment since the classes display dialogs to prompt the user for the parameters necessary for Dropbox.

As the figures show you are asked to specify key and secret for your application. The key and secret generated by Dropbox can be used to fill in the dialog input fields.

VFSLib Dropbox Application Key

Figure 1: Dropbox example application key dialog

VFSLib Dropbox Application Secret

Figure 2: Dropbox example application secret dialog

VFSLib Dropbox Access Token

Figure 3: Dropbox example access token/code

 

The next dialog is displayed (see figure 3) where you can enter the code (access token) from Dropbox. Simultaneously the example opens your favorite Internet browser showing the Dropbox website to get this code. Remember that it may be necessary to log in to Dropbox first. The access token is necessary to allow your application to access your Dropbox account.

If all input has been done the example will continue in the background by performing some simple file operations using the specified Dropbox account. Please check out the output of the example script.

 

Hint

If you already have an application key and secret as well as a working access token/code from Dropbox feel free to specify as main class params. The input dialogs will not be displayed any longer.

 


Step 3: The Dropbox Example Step By Step

Let's take a closer look at the Dropbox example implementation. The first thing to do is to configure the VFSLib class which is the central class of the VFSLib library:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Setup the main VFSLib instance, get evaluation license key from Leisenfels website
VFSLib vfslib = new VFSLib();
vfslib.setLicenseFile(licensefile); // Import from file

// We use the default file system manager here
DefaultFileSystemManager fsmanager = (DefaultFileSystemManager) VFS.getManager();

// Add Dropbox provider to VFS
DbxFileProvider provider = vfslib.addProviderDropbox(fsmanager, appkey, appsecret, 
 clientidentifier, language);
String scheme = vfslib.getSchemeDropbox();

// Pass access token over to VFSLib to access the Dropbox account
FileSystemOptions options = new FileSystemOptions();
DbxFileSystemConfigBuilder builder = new DbxFileSystemConfigBuilder();
builder.setAccessToken(options, token);

// Setup proper account name, the user name for Dropbox URLs (default: anonymous)
// dropbox://[name]:[token]@dropbox.com
String username = "vfslibtest";
builder.setAccountDisplayName(options, username);

Configuration of the VFSLib (see ExampleDropbox.java)

 

List a Dropbox Folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
String uri = scheme + "://" + username + "@dropbox.com";
try {
 FileObject fileobj = fsmanager.resolveFile(uri, options);
 if (fileobj.getType().equals(FileType.FOLDER)) {
 FileObject[] children = fileobj.getChildren();
 for (FileObject next : children) {
 if (next.getType().equals(FileType.FOLDER)) {
 System.out.println(" FOLDER: " + String.valueOf(next));
 }
 else System.out.println(" FILE:   " + String.valueOf(next));
 }
 }
 else System.out.println(" Entry " + uri + " is not a folder");
}
catch (Exception e) { e.printStackTrace(); }

List a Dropbox folder (see ExampleDropbox.java)

 

Create a Dropbox Folder

1
2
3
4
5
6
7
8
9
10
String tempfolder = scheme + "://" + username + "@dropbox.com/0123456789abcdefghijklmnopqrstuvwxyz";
boolean success = false;
try {
 FileObject fileobj = fsmanager.resolveFile(tempfolder, options);
 if (!fileobj.exists()) {
 fileobj.createFolder();
 success = fileobj.exists();
 }
}
catch (Exception e) { e.printStackTrace(); }

Create a Dropbox folder (see ExampleDropbox.java)

 

Upload a File To Dropbox, Get File Type, Last Modified, And Size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
String content = "Hello world!";
String encoding = "ISO-8859-1";

// Upload a file to Dropbox. Get file type, last modified, and size.
String tempfile = tempfolder + "/readme.txt";
try {
 FileObject fileobj = fsmanager.resolveFile(tempfile, options);
 fileobj.getContent().setAttribute(VFSLibConstants.ATTR_CONTENT_LENGTH, content.getBytes(encoding).length);
 OutputStream ostream = fileobj.getContent().getOutputStream();
 ostream.write(content.getBytes(encoding));
 ostream.flush();
 ostream.close();

 success = fileobj.exists();

 System.out.println(" TYPE: " + fileobj.getType().getName());
 System.out.println(" MOD: " + new Date(fileobj.getContent().getLastModifiedTime()));
 System.out.println(" SIZE: " + fileobj.getContent().getSize());
}
catch (Exception e) { e.printStackTrace(); }

Upload a file to Dropbox, get file type, last modified, and size (see ExampleDropbox.java)

 

Rename a Dropbox File

1
2
3
4
5
6
7
8
9
String tempfilerenamed = tempfolder + "/README";
try {
 FileObject fileobj = fsmanager.resolveFile(tempfile, options);
 FileObject fileobjrenamed = fsmanager.resolveFile(tempfilerenamed, options);
 fileobj.moveTo(fileobjrenamed);
 success = fileobjrenamed.exists();
 if (success) tempfile = tempfilerenamed;
}
catch (Exception e) { e.printStackTrace(); }

Rename a Dropbox file (see ExampleDropbox.java)

 

Download a File From Dropbox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
try {
 FileObject fileobj = fsmanager.resolveFile(tempfile, options);
 ByteArrayOutputStream bostream = new ByteArrayOutputStream();
 InputStream istream = fileobj.getContent().getInputStream();
 int len;
 byte[] buffer = new byte[1024];
 while ((len = istream.read(buffer)) != -1) {
 bostream.write(buffer, 0, len);
 }
 istream.close();
 bostream.flush();
 bostream.close();
 String loaded = new String(bostream.toByteArray(), encoding);

 success = loaded.equals(content);
}
catch (Exception e) { e.printStackTrace(); }

Download a file from Dropbox (see ExampleDropbox.java)

 

Remove a Dropbox File And Folder

1
2
3
4
5
6
7
8
9
10
try {
 // Remove file
 FileObject fileobj = fsmanager.resolveFile(tempfile, options);
 success = fileobj.delete();

 // Remove folder
 fileobj = fsmanager.resolveFile(tempfolder, options);
 success = fileobj.delete();
}
catch (Exception e) { e.printStackTrace(); }

Remove a Dropbox file and folder (see ExampleDropbox.java)

 


How to continue

 

Print Email


Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
Ok