Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
Hi All
Inorder to download the attachment ContentServerHelper class is used, but the method is throwing null pointer exception.
Here the The ContentServerHelper.service.findContentStream(Applicationdata) throws null pointer exception.
Please find the below code snippet for reference
QuerySpec qs = new QuerySpec(wt.doc.WTDocument.class);
SearchCondition sc = new SearchCondition(wt.doc.WTDocument.class,"master>number","=","0000078558");
qs.appendWhere(sc);
QueryResult qr = PersistenceHelper.manager.find((wt.pds.StatementSpec)qs);
while(qr.hasMoreElements())
{
WTDocument wtp = (WTDocument)qr.nextElement();
ObjectReference or = ObjectReference.newObjectReference(wtp);
ContentItem holder = ContentHelper.service.getPrimaryContent(or);
if (holder!=null)
{
ApplicationData applicationdata = (ApplicationData)holder;
try {
String fName=applicationdata.getFileName();
System.out.println(fName);
InputStream inputstream = ContentServerHelper.service.findContentStream(applicationdata);
int i = 0;
String filepath="D:\\exportfiles";
File temp=new File(filepath+File.separator+fName);
String docfile = temp.getAbsolutePath();
FileOutputStream tout = new FileOutputStream(docfile);
byte abyte0[] = new byte[2048];
while ( (i = inputstream.read(abyte0, 0, abyte0.length)) >= 0)
{
tout.write(abyte0, 0, i);
}
tout.close();
} catch (WTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
you can use below API to get the attachment of WTDocument.
WTDocument document=null;
QueryResult attachments = ContentHelper.service.getContentsByRole(document, ContentRoleType.SECONDARY);
while (attachments.hasMoreElements()) {
ApplicationData appData = (ApplicationData) attachments.nextElement();
// String url = ContentHelper.getDownloadURL(document, appData).toString();
}
Hope this helps !!
Thanks,
Shreyas
Hi Shreyas
Using the code i am able to genarate a link which displays a sublink for downloading.The requirement to generate the link(sub link here) to download the attachment programtically.
Best Regards
Kumar
You may need to use this API before you call the others
ContentHolder holder = ContentHelper.service.getContents(holder);
It sets some stuff up internally,
Simon
Hi Simon
As u say we have to set some stuff up internally as ContentServerHelper.service is just a interface .ContentHelper.service.getContents(holder); is not working and still throwing nullpointer.I tried ContentServerHelper.service.update method but i m not able to initialize it.
The above program throws a Warning
WARNING: The ManagerService is not initialized! This can be caused by:
1) Attempting to invoke a server only method from a remote client
2) Attempting to invoke a method on a service from the constructor or static initializer of another service
Exception in thread "main" java.lang.ExceptionInInitializerError
Is there anyway to initialize ContentServerHelper.service
Best Regards
Kumar