Timo Ernst
Timo Ernst

Reputation: 15973

Why does Dropbox server not respond when trying to upload a file via its API?

I am using the official Dropbox API for Java. So far, everything works smoothly. Authentication via oauth works and so do other functions (like directory listings).

Now, I tried to upload a file like this:

InputStream is = getInputStream();
byte[] bytes = is2Bytes(is); // Gets all bytes "behind" the stream
int len = bytes.length;
api.putFileOverwrite(path, is, len, null);

Now, when I do this call, my application hangs for about 15 seconds and then I get an exception thrown that Dropbox server did not respond.

So, first I asked Dropbox support if there was something wrong with their server. There isn't.

Then, I played around with the parameters of the putFileOverwrite method and I found out that if I set len=0 manually, the server responds and creates a 0 byte file with the correct file name.

As another test, I manually entered the value len=100 (the original file has 250KB so that should be ok). Again, the server does NOT respond.

So, what's wrong?

Upvotes: 0

Views: 422

Answers (2)

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

That is not weird at all. Since you use your self-made method is2Bytes, the steam is empty, because you read all the bytes to count them. The proper way of doing this would be either knowing how many bytes you are going to send or using the build-in method for sending a file.

public HttpResponse putFile(String root, String dbPath, File localFile)

Upvotes: 3

Timo Ernst
Timo Ernst

Reputation: 15973

Very weird. I was able to work around this by re-creating a new InputStream from the byte array and send that to Dropbox:

InputStream is = getInputStream();
byte[] bytes = is2Bytes(is); // Gets all bytes "behind" the stream
int len = bytes.length;
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
api.putFileOverwrite(path, bis, len, null);

Upvotes: 0

Related Questions