1

Could you point me out to a code or url where I can find some examples how to use dropbox java api and upload binary files like, .doc files jpg and video files.

Current examples in the web only point to uploading a text file. But when I try to read files using java InputStream and convert them to byte array and pass into dropbox file upload functions files get corrupted. Same issue with downloading files as well. Thanks in Advance.

Regards, Waruna.

EDIT-- Code Sample

FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte [] buf = new byte[1024];
for(int readNum; (readNum = fis.read(buf)) != -1;) {
    bos.write(buf, 0, readNum);
    System.out.println("read "+ readNum + "bytes,");
}

ByteArrayInputStream inputStream2 = new ByteArrayInputStream(bos.toByteArray());

Entry newEntry = mDBApi.putFile("/uploads/"+file.getName(), inputStream2, file.toString().length(), null, null);
System.out.println("Done. \nRevision of file: " + newEntry.rev + " " + newEntry.mimeType);
return newEntry.rev;
2
  • Post your code, so we can suggest you where it is wrong! Commented Apr 14, 2012 at 12:37
  • Hi, thanks, I update the question with my code. Commented Apr 14, 2012 at 23:51

2 Answers 2

1

The 3rd argument of DropboxAPI.putFile() should be the number of bytes to read from the input stream - You are passing the length of the filename.

Instead of

Entry newEntry = mDBApi.putFile("/uploads/"+file.getName(), inputStream2,
            file.toString().length(), null, null);

Use

Entry newEntry = mDBApi.putFile("/uploads/"+file.getName(), inputStream2,
            bos.size(), null, null);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, this work perfectly. I lost quite a lot of development time because of this small mistake.
0

I don't think you need to convert to byte array, simply use FileInputStream is enough for a file, txt as well as binary. The following code works, I just tested with JPG.

    DropboxAPI<?> client = new DropboxAPI<WebAuthSession>(session);

    FileInputStream inputStream = null;
    try {
        File file = new File("some_pic.jpg");
        inputStream = new FileInputStream(file);
        DropboxAPI.Entry newEntry = client.putFile("/testing.jpg", inputStream,
                file.length(), null, null);
        System.out.println("The uploaded file's rev is: " + newEntry.rev);
    } catch (DropboxUnlinkedException e) {
        // User has unlinked, ask them to link again here.
        System.out.println("User has unlinked.");
    } catch (DropboxException e) {
        System.out.println("Something went wrong while uploading.");
    } catch (FileNotFoundException e) {
        System.out.println("File not found.");
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {}
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.