Reputation: 4647
I have written a method that encodes a given PDF-file into a byte array:
public static byte[] encodeFileToBase64(String pathToPdfFile)
throws IOException {
File file = new File(pathToPdfFile);
InputStream input = null;
try {
input = new FileInputStream(file);
} catch (FileNotFoundException e) {
throw (e);
}
byte[] buffer = new byte[(int) file.length()];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
try {
while ((bytesRead = input.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw (e);
}
input.close();
return baos.toByteArray();
}
Now I am implementing an interface to an SOAP webservice.
The user manual demands a PDF file in Base64 encoding.
I have generated Java code from the given wsdl
file with Apache Axis2 (wsdl2java
). In this code, it is required to set the given PDF file as javax.activation.DataHandler
:
/**
* Auto generated setter method
* @param param PdfDocument
*/
public void setPdfDocument(javax.activation.DataHandler param) {
this.localPdfDocument = param;
}
Now, my question is, how to get the Base64 encoded stuff into a DataHandler
.
Can you help me?
Thank you!
Upvotes: 1
Views: 6672
Reputation: 81
try this :
DataSource fds = new FileDataSource("filePath");
request.setMessageFile(new DataHandler(fds));
the javax.activation.* package handle the base64 encoding natively.
Upvotes: 2