It's all about the answers!

Ask a question

how to use java api (RTC ClientLib4.0) to download attachment?


yu xin liang (322) | asked Jan 06 '13, 11:47 p.m.
I have already wrote this code:

                List<IReference> wang=refs.getReferences(WorkItemEndPoints.ATTACHMENT);
                System.out.println("Jerry now!");
                for(IReference ref:wang){
                    IAttachmentHandle ath=(IAttachmentHandle)ref.resolve();
                    IAttachment attachment=clientA1.resolveAuditable(ath,IAttachment.FULL_PROFILE,monitor);
                    System.out.println(attachment.getName());
                    IContent content=attachment.getContent();
                    System.out.println(content);

but now I want to down load the attachment file to my server on which this code will be run, but I only got an object of IAttachment and then IContent, I really do not know how to write such codes, could you help me?

Thanks!
                   
                }

Accepted answer


permanent link
Ralph Schoon (63.5k33646) | answered Jan 07 '13, 12:56 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
yu xin liang selected this answer as the correct answer

Comments
yu xin liang commented Jan 07 '13, 1:20 a.m.

Hi schoon, thanks for your rapid response, but I cannot open that link, is that right? thanks a lot!


Ralph Schoon commented Jan 07 '13, 2:53 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Hi Yu, apparently wordpress is blocked in china. This is what we found as a workaround:

"The advantage with blogs is that they all provide RSS feeds so you can easily read any blog using a web based feed readers even if the main WordPress site is blocked.
In case of WordPress blogs, you can append the string ".nyud.net" to the blog URL and it should open just fine. For instance, if the main blog is located at labnol.wordpress.com, you can access a mirror image of this site from labnol.wordpress.com.nyud.net."

We have seen http://rsjazz.wordpress.com.nyud.net/ is accessible through Hong Kong. If that does not work for you I will post the content in another answer below.


Ralph Schoon commented Jan 07 '13, 5:09 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Hi Yu, please let me know if the work around is working for you. Thanks!

One other answer



permanent link
Ralph Schoon (63.5k33646) | answered Jan 07 '13, 2:53 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Our lawyers reminded me to state that the code in this post is derived from examples from Jazz.net as well as the RTC SDK. The usage of code from that example source code is governed by this license. Therefore this code is governed by this license, which basically means you can use it for internal usage, but not sell. Remember that this code comes with the usual lack of promise or guarantee. Enjoy!

The example below is based on the wiki entry on Programmatic Work Item Creation. I basically use the main() and run() operation for the parameter handling. The code below is inserted at the end of the run() operation. The WorkItemOperation part is not used in this case.

The code below shows how to get to the attachments resolving the work item references and using the endpoint to narrow down to the attachments. See the post about manipulation references using the Plain Java Client Library for more details on references.

Once the IAttachment is resolved it is passed to the saveAttachment() method which does the download and saving part.


IWorkItem workItem = workItemClient.findWorkItemById(id,
    IWorkItem.FULL_PROFILE, null);

IWorkItemCommon common = (IWorkItemCommon) teamRepository
    .getClientLibrary(IWorkItemCommon.class);
IWorkItemReferences workitemReferences = common
    .resolveWorkItemReferences(workItem, null);
List references = workitemReferences
    .getReferences(WorkItemEndPoints.ATTACHMENT);
for (IReference iReference : references) {
    IAttachmentHandle attachHandle = (IAttachmentHandle) iReference
        .resolve();
    IAuditableClient auditableClient = (IAuditableClient) teamRepository
        .getClientLibrary(IAuditableClient.class);
    IAttachment attachment = (IAttachment) auditableClient
        .resolveAuditable((IAttachmentHandle) attachHandle,
            IAttachment.DEFAULT_PROFILE, null);
    saveAttachment(teamRepository, attachment);
}

The saveAttachment() method basically creates a new file with the name stored in the attachment and uses the ITeamRepository.contentManager() to get the content into the file. The code below uses the file name stored in the attachment for simplicity, but you can provide a different name and location.


/**
* Save the attachment
*/
public static void saveAttachment(ITeamRepository teamRepository,
        IAttachment attachment) throws TeamRepositoryException {
    try {
        File save = new File(attachment.getName());

        OutputStream out = new FileOutputStream(save);
        try {
            teamRepository.contentManager().retrieveContent(
                attachment.getContent(), out, null);
        } finally {
            out.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
}

As usual the exception handling is very basic and you might want to improve that if using this code. I hope this is helpful and saves some cycles searching through the RTC SDK.

Your answer


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.