Waruna Waruna - 1 month ago 12
Java Question

Uploading binary files using dropbox java api

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;

Answer

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);
Comments