Skip to content

Instantly share code, notes, and snippets.

@Chetan496
Created December 16, 2016 12:22
Show Gist options
  • Save Chetan496/22386fd54cfae6444d3a958c4f2cd035 to your computer and use it in GitHub Desktop.
Save Chetan496/22386fd54cfae6444d3a958c4f2cd035 to your computer and use it in GitHub Desktop.
/*An example of uploading a document to the KC */
public void uploadDocumentToAppianKC(
ContentService contentService,
byte[] bytes,
String extFileName,
Long folderId,
String uniqueIdentifier) throws InvalidContentException, StorageLimitException, PrivilegeException,
InsufficientNameUniquenessException, DuplicateUuidException, IOException {
if (bytes.length > 0) {
Document newDocument = new Document();
newDocument.setExternalFilename(extFileName);
final String fileExtention = extFileName.substring(extFileName.lastIndexOf('.') + 1);
try {
String folderName = contentService.getVersion((Long) folderId, ContentConstants.VERSION_CURRENT)
.getDisplayName();
} catch (InvalidVersionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (extFileName.lastIndexOf('.') > 0) {
if (extFileName.lastIndexOf('\\') > 0) {
newDocument.setName(
extFileName.substring(extFileName.lastIndexOf('\\') + 1, extFileName.lastIndexOf('.')));
} else {
newDocument.setName(extFileName.substring(0, extFileName.lastIndexOf('.')));
}
} else if (extFileName.lastIndexOf('\\') > 0) {
newDocument.setName(extFileName.substring(extFileName.lastIndexOf('\\') + 1, extFileName.length() - 1));
} else {
newDocument.setName(extFileName);
}
newDocument.setParent(folderId.longValue());
if (newDocument.getExternalFilename().lastIndexOf('.') > 0) {
newDocument.setExtension(fileExtention);
}
newDocument.setSize(bytes.length);
newDocument.setDescription("Unique Identifier: " + uniqueIdentifier);
ContentOutputStream stream = contentService.upload(newDocument, ContentConstants.UNIQUE_NONE);
stream.write(bytes);
stream.flush();
stream.close();
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment