Page 1 of 1
About Question enthuware.ocejws.v6.2.140 :
Posted: Sun Apr 06, 2014 5:40 pm
by himaiMinh
AttachmentPart attachment = soapMessage.createAttachment();
DataHandler dh = new FileDataHandler("picture.jpeg");
attachment.setContent(dh,"application/octet-stream");
I think this is the same as setting MTOM enabled, and the size of the attachment is more or less the same as the original data.
Re: About Question enthuware.ocejws.v6.2.140 :
Posted: Sun Apr 06, 2014 7:00 pm
by himaiMinh
The above code was a draft.
The jpeg image cannot be converted into application/octet-stream in the attachment when I implement it.
Here is what I tried :
Code: Select all
//This is a standalone program
public class SkiImageClient4 {
public static void main (String... args) throws SOAPException{
SOAPMessage soapmessage= MessageFactory.newInstance().createMessage();
AttachmentPart attachment = soapmessage.createAttachmentPart();
attachment.setContent(createImage1("nordic"), "image/jpeg");
InputStream input = attachment.getRawContent();
byte[] bytes = attachment.getRawContentBytes();
System.out.println("number of bytes in attachement : "+ bytes.length);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
...
}
// from Java web service up and running chapter 3.
private static Image createImage1(String name){
byte[] bytes = getRawBytes(name);
System.out.println("number of bytes after the image is created : "+ bytes.length);
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
Iterator iterators = ImageIO.getImageReadersByFormatName("jpg");
ImageReader iterator = (ImageReader) iterators.next();
try{
ImageInputStream iis = ImageIO.createImageInputStream(in);
iterator.setInput(iis, true);
return iterator.read(0);
}
catch (IOException e){
...
return null;
}
}
private static byte[] getRawBytes(String name){
ByteArrayOutputStream out = new ByteArrayOutputStream();
try{
String cwd = System.getProperty("user.dir");
String sep = System.getProperty("file.separator");
String base_name = cwd + sep + "jpegs"+ sep;
String file_name = base_name + name + ".jpg";
System.out.println(file_name);
FileInputStream in = new FileInputStream(file_name);
byte[] buffer = new byte[2048];
int n = 0;
while ((n=in.read(buffer))!= -1){
System.out.print(" n "+ n);
out.write(buffer, 0,n);
}
in.close();
}
catch (IOException e){ ...}
return out.toByteArray();
}
}
Output from NetBean Platform:
number of bytes after the image is created : 682
number of bytes in attachement : 666
line ����
Attaching the image as raw byte in a SOAPMessage's attachment part, the size of the data sent to the server will be more or less the same as the original size of the data.
Re: About Question enthuware.ocejws.v6.2.140 :
Posted: Mon Apr 07, 2014 9:21 am
by fjwalraven
Good catch!
You have got a valid point here. I will come back to this thread with my update (I have to verify where I have used this also in other questions).
Thanks for your feedback!
Regards,
Frits