It's all about the answers!

Ask a question

How to use createAttachment to upload attachments on server using RTC Java API


Yang Xu (134) | asked Oct 10 '19, 6:49 a.m.

 Hi,

I was trying to upload attachments on RTC server using Java API (SDK).The following is the code I am using. After I executed it, I didn't get the result I wanted. Is there any problem with my code or is there any other way to implement it?
Thanks for the help.


public class GenerateReportAdvistor extends AbstractService implements
IOperationAdvisor {

public void run(AdvisableOperation operation,
IProcessConfigurationElement advisorConfiguration,
IAdvisorInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException {

Object data = operation.getOperationData();
if (data instanceof ISaveParameter) {
ISaveParameter saveParameter = (ISaveParameter) data;
IAuditable auditable = ((ISaveParameter) data).getNewState();
if (auditable instanceof IWorkItem) {
IWorkItem oldWorkItem = (IWorkItem) saveParameter.getOldState();
IWorkItem newWorkItem = (IWorkItem) auditable;
Identifier<IState> newState = newWorkItem.getState2();
IWorkItemServer service = getService(IWorkItemServer.class);
IRepositoryItemService repositoryItemService=getService(IRepositoryItemService.class);
ICombinedWorkflowInfos combinedWorkflowinfos = service.findCombinedWorkflowInfos(newWorkItem.getProjectArea(), monitor);
if(oldWorkItem!=null){
 try {
File attachmentFile = new File("C:\temp\test.doc");
FileInputStream fis = new FileInputStream(attachmentFile);
IAttachment newAttachment =service.createAttachment(projectArea, monitor);
newAttachment.setName("TestName");
IContent content= service.getAuditableCommon().storeContent(IContent.CONTENT_TYPE_UNKNOWN, IContent.ENCODING_UTF_8, fis, attachmentFile.length(), monitor);
newAttachment.setContent(content);
newAttachment=service.saveAttachment(newAttachment, monitor);
IItemReference reference = WorkItemLinkTypes.createAttachmentReference(newAttachment);
IWorkItemReferences sourceReferences= service.resolveWorkItemReferences(newWorkItem, monitor);
sourceReferences.add(WorkItemEndPoints.ATTACHMENT, reference);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
}

Be the first one to answer this question!


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.