I wish I had an overwrite option ...
//Upload file to root folder
BoxFile.Info newFileInfo = null;
try (FileInputStream stream = new FileInputStream("C:\\hoge\\updload_sample.txt")) {
  newFileInfo = rootFolder.uploadFile(stream, fileName);
} catch (BoxAPIResponseException e) {
  System.out.println("LocalizedMessage : " + e.getLocalizedMessage());
  //If a file with the same name already exists
  if (e.getLocalizedMessage().contains("409")) {
   //Search for the corresponding file
    Iterator<Info> ite = rootFolder.getChildren().iterator();
    while (ite.hasNext()) {
      Info boxInfo = (Info)ite.next();
      String objectName = boxInfo.getName();
      if(objectName.equals(fileName)) {
        String fileId = boxInfo.getID();
        BoxFile file = new BoxFile(api, fileId);
     //upload
        try (FileInputStream stream = new FileInputStream("C:\\hoge\\updload_sample.txt")) {
          file.uploadVersion(stream);
        }
      }
    }
  }
}
BoxFile.uploadVersion has been deprecated, but I couldn't find any other alternative. ..
Recommended Posts