0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

BOX SDK(java)で同名ファイルの上書きアップロード

Posted at

BoxFolder.uploadFileだと既に同名のファイルが存在する場合にエラーになる

上書きオプションとかあればいいんですが・・・。

// ファイルをルートフォルダにアップロード
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 (e.getLocalizedMessage().contains("409")) {
   // 該当ファイルを捜索
    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);
     // アップロード
        try (FileInputStream stream = new FileInputStream("C:\\hoge\\updload_sample.txt")) {
          file.uploadVersion(stream);
        }
      }
    }
  }
}

課題

BoxFile.uploadVersion は非推奨になっているんですが他に代替手段を見つけられなかった。。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?