public void uploadToDropbox(String fileName, DbxClient dbxClient)
     throws DbxException, IOException {
   File inputFile = new File(fileName);
   FileInputStream fis = new FileInputStream(inputFile);
   try {
     DbxEntry.File uploadedFile =
         dbxClient.uploadFile("/" + fileName, DbxWriteMode.add(), inputFile.length(), fis);
     String sharedUrl = dbxClient.createShareableUrl("/" + fileName);
     System.out.println(sharedUrl);
   } finally {
     fis.close();
   }
 }
 @Override
 protected final void call(final DbxClient client, final ProgressMonitor pm) throws DbxException {
   pm.begin(1);
   if (client.createFolder(path) == null) {
     getLog().error("already a folder at the given destination");
   }
 }
 public void listDropboxFolders(String folderPath) throws DbxException {
   DbxEntry.WithChildren listing = dbxClient.getMetadataWithChildren(folderPath);
   System.out.println("Files List:");
   for (DbxEntry child : listing.children) {
     System.out.println("	" + child.name + ": " + child.toString());
   }
 }
 /* returns Dropbox size in GB */
 public long getDropboxSize() throws DbxException {
   long dropboxSize = 0;
   DbxAccountInfo dbxAccountInfo = dbxClient.getAccountInfo();
   // in GB :)
   dropboxSize = dbxAccountInfo.quota.total / 1024 / 1024 / 1024;
   return dropboxSize;
 }
 String getAccount() {
   try {
     account = "Linked account: " + client.getAccountInfo().displayName;
   } catch (DbxException ex) {
     Logger.getLogger(ReadFileFromFolder.class.getName()).log(Level.SEVERE, null, ex);
   }
   return account;
 }
  public void downloadFromDropbox(String fileName, DbxClient dbxClient)
      throws DbxException, IOException {
    FileOutputStream outputStream = new FileOutputStream(fileName);
    try {
      DbxEntry.File downloadedFile = dbxClient.getFile("/" + fileName, null, outputStream);

      System.out.println("Metadata: " + downloadedFile.toString());
    } finally {
      outputStream.close();
    }
  }
  public void doBrowse(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    if (!common.checkGet(request, response)) return;
    User user = common.getLoggedInUser(request);
    if (user == null) {
      common.pageSoftError(response, "Can't do /browse.  Nobody is logged in.");
      return;
    }

    DbxClient dbxClient = requireDbxClient(request, response, user);
    if (dbxClient == null) return;

    // Make sure the path starts with '/'.  There are probably other checks we can perform...
    String path = request.getParameter("path");
    if (path == null) {
      path = "/";
    } else {
      String pathError = DbxPath.findError(path);
      if (pathError != null) {
        response.sendError(400, "Invalid path: " + jq(path) + ": " + pathError);
        return;
      }
    }

    // Get the folder listing from Dropbox.
    DbxEntry.WithChildren listing;
    try {
      listing = dbxClient.getMetadataWithChildren(path);
    } catch (DbxException ex) {
      common.handleDbxException(response, user, ex, "getMetadataWithChildren(" + jq(path) + ")");
      return;
    }

    if (listing == null) {
      response.sendError(400, "Path doesn't exist on Dropbox: " + jq(path));
    }

    FormProtection fp = FormProtection.start(response);

    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), UTF8));

    out.println("<html>");
    out.println("<head><title>" + escapeHtml4(path) + "- Web File Browser</title></head>");
    out.println("<body>");
    fp.insertAntiRedressHtml(out);

    out.println("<h2>Path: " + escapeHtml4(path) + "</h2>");

    if (listing == null) {
      out.println("<p>Nothing here...</p>");
    }
    // Folder
    else if (listing.entry instanceof DbxEntry.Folder) {
      // Upload form
      out.println("<form action='/upload' method='post' enctype='multipart/form-data'>");
      fp.insertAntiCsrfFormField(out);
      out.println("<label for='file'>Upload file:</label> <input name='file' type='file'/>");
      out.println("<input type='submit' value='Upload'/>");
      out.println(
          "<input name='targetFolder' type='hidden' value='"
              + escapeHtml4(listing.entry.path)
              + "'/>");
      out.println("</form>");
      // Listing of folder contents.
      out.println("<ul>");
      for (DbxEntry child : listing.children) {
        out.println(
            "  <li><a href='/browse?path="
                + escapeHtml4(child.path)
                + "'>"
                + escapeHtml4(child.name)
                + "</a></li>");
      }
      out.println("</ul>");
    }
    // File
    else {
      DbxEntry.File f = (DbxEntry.File) listing.entry;
      out.println("<pre>");
      out.print(escapeHtml4(f.toStringMultiline()));
      out.println("</pre>");
    }

    out.println("</body>");
    out.println("</html>");

    out.flush();
  }
  public void doUpload(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    if (!common.checkPost(request, response)) return;
    User user = common.requireLoggedInUser(request, response);
    if (user == null) return;

    DbxClient dbxClient = requireDbxClient(request, response, user);
    if (dbxClient == null) return;

    try {
      request.getParts(); // Just call getParts() to trigger the too-large exception.
    } catch (IllegalStateException ex) {
      response.sendError(400, "Request too large");
      return;
    }

    String targetFolder = slurpUtf8Part(request, response, "targetFolder", 1024);
    if (targetFolder == null) return;

    Part filePart = request.getPart("file");
    if (filePart == null) {
      response.sendError(400, "Field \"file\" is missing.");
      return;
    }
    String fileName = filePart.getName();
    if (fileName == null) {
      response.sendError(400, "Field \"file\" has no name.");
      return;
    }

    // Upload file to Dropbox
    String fullTargetPath = targetFolder + "/" + fileName;
    DbxEntry.File metadata;
    try {
      metadata =
          dbxClient.uploadFile(
              fullTargetPath, DbxWriteMode.add(), filePart.getSize(), filePart.getInputStream());
    } catch (DbxException ex) {
      common.handleDbxException(response, user, ex, "uploadFile(" + jq(fullTargetPath) + ", ...)");
      return;
    } catch (IOException ex) {
      response.sendError(400, "Error getting file data from you.");
      return;
    }

    // Display uploaded file metadata.
    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));

    out.println("<html>");
    out.println("<head><title>File uploaded: " + escapeHtml4(metadata.path) + "</title></head>");
    out.println("<body>");
    out.println("<h2>File uploaded: " + escapeHtml4(metadata.path) + "</h2>");
    out.println("<pre>");
    out.print(escapeHtml4(metadata.toStringMultiline()));
    out.println("</pre>");
    out.println("</body>");
    out.println("</html>");

    out.flush();
  }
 public void createFolder(String folderName) throws DbxException {
   dbxClient.createFolder("/" + folderName);
 }