예제 #1
0
 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();
   }
 }
  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();
  }