- Home
- Binder
- DatatypeConverter
- JAXB
- JAXBContext
- JAXBElement
- JAXBElement.GlobalScope
- JAXBIntrospector
- Marshaller.Listener
- SchemaOutputResolver
- Unmarshaller.Listener
- Javax.xml.bind.util classes
- JAXBResult
- JAXBSource
- ValidationEventCollector
- Javax.xml.parsers classes
- DocumentBuilder
- DocumentBuilderFactory
- SAXParser
- SAXParserFactory
- Javax.xml.soap classes
- AttachmentPart
- MessageFactory
- MimeHeader
- MimeHeaders
- SAAJMetaFactory
- SOAPConnection
- SOAPConnectionFactory
- SOAPFactory
- SOAPMessage
- SOAPPart
- Javax.xml.validation classes
- Schema
- SchemaFactory
- TypeInfoProvider
- Validator
- ValidatorHandler
- Javax.xml.xpath classes
- XPathConstants
- XPathFactory
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
AttachmentPart setRawContentBytes() Method
Description
The Javax.xml.soap.AttachmentPart.setRawContentBytes(byte[] content, int offset, int len, String contentType) method sets the content of this attachment part to that contained by the byte[] array content and sets the value of the Content-Type header to the value contained in contentType.
Declaration
Following is the declaration for javax.xml.soap.AttachmentPart.setRawContentBytes() method
public abstract void setRawContentBytes(byte[] content, int offset, int len, String contentType)
Parameters
content − the raw data to add to the attachment part
offset − the offset in the byte array of the content
len − the number of bytes that form the content
contentType − the value to set into the Content-Type header
Return Value
This method does not return a value.
Exception
SOAPException − if an there is an error in setting the content
Example
The following example shows the usage of javax.xml.soap.AttachmentPart.setRawContentBytes() method.
package com.tutorialspoint;
import com.sun.xml.internal.messaging.saaj.util.Base64;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
import java.io.InputStream;
import java.util.Iterator;
import javax.activation.DataHandler;
import javax.xml.soap.*;
public class AttachmentPartDemo {
public static void main(String[] args) {
try {
// create a new SOAPMessage
SOAPMessage message = MessageFactory.newInstance().createMessage();
// create a string as a new attachment
String attachment = "This is an attachment";
// get the data handler and assign the attachment
DataHandler handler = new DataHandler(attachment, "HandlerHeader");
// create the attachment part
AttachmentPart attachPart = message.createAttachmentPart();
// add raw content
byte[] encoded = Base64.encode(attachment.getBytes());
attachPart.setRawContentBytes
(encoded, 0, encoded.length, "plain/text");
// add the attachment part in the message
message.addAttachmentPart(attachPart);
// read the raw content as bytes
byte[] content = attachPart.getRawContentBytes();
for (int i = 0; i < content.length; i++) {
System.out.print("" + (char) content[i]);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
If we compile the code and execute it, this will produce the following result −
VGhpcyBpcyBhbiBhdHRhY2htZW50