+ Start a Discussion
Force.comForce.com 

Fetch attachment body of Salesforce from Java

I am fetching attachment object from java. So, I am getting the Attachement Body in base64 type. 

How can I get complete body of attachment in Java. 

 

Please suggest.

Best Answer chosen by Admin (Salesforce Developers) 
_Prasu__Prasu_

You can simple read the field value convert it into byte[] and write it to the file with the name of Attachment.

All Answers

_Prasu__Prasu_

You can simple read the field value convert it into byte[] and write it to the file with the name of Attachment.

This was selected as the best answer
Force.comForce.com

Thanks a lot.

I did the same and it worked.

NimsNims

Hii,

     how to get the Account object attachment file from salesforce through java.......,Plss help me

 

Thanks & Regards..,

 

Nims

SuperfellSuperfell

you'd use a soql query, something like select body from attachment where parentId = 'some account id'

NimsNims

Hi Simon thank you...,

simon i want,we have some attachment files in account object.now we get that attachment file and that file stores in our local machine like desktop using java application. so pls snd me java code


Regards,
Nims

Force.comForce.com

Hi Nims,

 

Try this java code for fetching attachments of Account.

QueryResult queryResultsAttach = connection.query("SELECT Id,Body,Name,ParentId FROM Attachment where ParentId='"+pId+"');
	       

 Thanks,

Pragati

NimsNims

Thank you Pragathi I will try.......

 

Regards,

Nims.

NimsNims

Hi Pragathi This query is correct,i applyed this query,file was attached but i want also that attachment file is store in our local machine how to store plss tell me...

Thanks & Regards,
Nims.

Force.comForce.com

Hi Nims,

 

Try this out

 

if (queryResultsAttach.getSize() > 0) {
for (int i=0;i<queryResultsAttach.getRecords().length;i++) {
    q = (Attachment)queryResultsAttach.getRecords()[i];
    int size = q.getBody().length;
    f1=new File("C:/"+q.getName());
    byte[] buffer= new byte[size];
    buffer = q.getBody();
    FileOutputStream output = new FileOutputStream(f1);
    output.write(buffer);
}
}

  Thanks,

Pragati

NimsNims

Hi Pragati,

This fn is correct,its working....,Thank you Pragati Thank you very much.......

Force.comForce.com

Happy to help.

 

Thanks,

Pragati

NimsNims

HI Pragati,

this fn is working,i got the attachment file,Thank you Pragati Thank you sooo much..

 

Regards,

Nims.

montmorency_143montmorency_143

Hello Pragati,

I am  trying the to convert body of attachment object back into the pdf using java 

my code is

 byte[] bnew1=new byte[bodyLength];
    bnew=org.apache.shiro.codec.Base64.decode(pdfBody);
    bnew1=org.apache.shiro.codec.Base64.decode(bnew);

where pdfBody is base 64 encoded string (body from attachment )

and finaly am converting this byte array to InputStram and passing as response to struts actoion,but i am getting error as 

Failed to load pdf document.

 

Please help ...thanks in advance

Force.comForce.com

Hi Montmorency_143,

 

I converted the Attachment into PDF using these steps. Check if these can help you.

 

List<InputStream> pdfs;
pdfs.add(new FileInputStream(<attachment file>));
List<PdfReader> readers = new ArrayList<PdfReader>();
int totalPages = 0;
Iterator<InputStream> iteratorPDFs = pdfs.iterator();

// Create Readers for the pdfs.
while (iteratorPDFs.hasNext()) {
InputStream pdf = iteratorPDFs.next();
PdfReader pdfReader = new PdfReader(pdf);
readers.add(pdfReader);
totalPages +=pdfReader.getNumberOfPages();
}
// Create a writer for the outputstream
// Loop through the PDF files and add to the output.

 

Cheers,

Pragati

 

 

 

 

Saranyan_NarayananSaranyan_Narayanan

import com.sforce.ws.util.Base64;

 

and use 

String s1= (String)s.getField("Body");
FileOutputStream output = new FileOutputStream(f1);
output.write(Base64.decode(s1.getBytes()));

 

( This is how I did for partner wsdl)  , can use for Enterprise WSDL as well

Sunil PalSunil Pal
Hi All,

I am fetching the attachment in java and I need to pass it to Box serevr, for that I am using "FileInputStream stream1 = new FileInputStream(filepath); " But I need to pass the file which is stored as attachment. 
But not able to pass the file location as given. As Prasanna__d has suggested the solution. Can you please share some code snippet for the this. It will be very usefull.

Thanks
Sunil
shekar B Nshekar B N
Hi 
Pragati

Whats is q variable above code
what it holds

q = (Attachment)queryResultsAttach.getRecords();