ByteArrayDataSource ds = new ByteArrayDataSource();
ds.setContentType("image/jpeg");
ds.setBytes(bytes);
DataHandler dh = new DataHandler(ds);Here's some sample code for a DataSource. I just typed this in without checking, your mileage may vary. Apart from the OutputStream - which you don't need - this is trivial stuff.
public class ByteArrayDataSource implements javax.activation.DataSource {
private byte[] bytes;
public void setBytes(byte[] bytes) { this.bytes = bytes; }
public byte[] getBytes() { return bytes; }
private String contentType;
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentType() { return contentType; }
public InputStream getInputStream() {
return new ByteArrayInputStream(bytes);
}
/** for completeness, here's how to implement the outputstream.
this is unnecessary for what you're doing, you can just throw
an UnsupportedOperationException. */
public OutputStream getOutputStream() {
final ByteArrayDataSource bads = this;
final ByteArrayOutputStream baos =
new ByteArrayOutputStream();
// return an outputstream that sets my byte array
// when it is closed.
return new FilterOutputStream(baos) {
public void close() throws IOException {
baos.close();
bads.setBytes(baos.toByteArray());
}
}
}
}subhendukumar mohanty wrote:
Hi all,
Our Backend service will call a application which will return the image in byte array. Our Backend is exposed as a webservice for the client. How to create a datahandler object from the byte array.
Thanks, Subhendu
