Upload file to DropBox using REST api
Hey.. this is actually post related to Dropbox API section. In this post i'm going to show you how to upload a file to Dropbox using REST api by writing a simple JAVA code.
All you need to do is first obtain an access token from Dropbox. Then just do a PUT call to dropbox to url https://api-content.dropbox.com/1/files_put/root/path . Actually Dropbox api support both POST and PUT call as to store file on dropbox file system but it's recommended to use PUT instead of POST method.
Here root is root of file system and dropbox support dropbox and sandbox as root. And in path, any path where you want to store file corresponding to root on dropbox must be given.
Note: here it is necessary that path must not point to any folder. Otherwise you will receive error from dropbox when you try to store file.
Now to upload file to dropbox required file's bytes send to it. And it's mendetory that call body must contain only required file's bytes nothing else.
Now required Java code should look something like this:
String sourcePath = ""; //required file path on local file system Path pathFile = Paths.get(sourcePath); byte[] data = Files.readAllBytes(pathFile); URL url = new URL("https://api-content.dropbox.com/1/files_put/${root}/${destinationPath}?access_token=${accessToken}"); HttpURLConnection connection; try { connection = (HttpURLConnection)url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", "mime/type"); connection.setRequestProperty("Content-Length", String.valueOf(data.length)); OutputStream outputStream = connection.getOutputStream(); outputStream.write(data); outputStream.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); //required output from dropbox } finally { connection.disconnect(); }












