Project Requirement
In this section we will develop SOAP application having a text attachment.
Then it retrieves the content of the attachment file and display it on the screen
Solution

Figure 1

Figure. 2
Creating the java Class

Figure. 3

Figure. 4
Change the content of java file
Now make the changes in the code of attach1.java in file as code given below.
package pack1;
import java.io.*;
import java.util.Iterator;
import javax.xml.soap.*;
public class attatch1 {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
String line = "";
try {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
header.detachNode();
AttachmentPart attachment1 = message.createAttachmentPart();
fr = new FileReader(new File("rose1.txt"));
br = new BufferedReader(fr);
String stringContent = "";
line = br.readLine();
while (line != null) {
stringContent = stringContent.concat(line);
stringContent = stringContent.concat("\n");
line = br.readLine();
}
attachment1.setContent(stringContent, "text/plain");
attachment1.setContentId("attached_text");
message.addAttachmentPart(attachment1);
Iterator iterator = message.getAttachments();
while (iterator.hasNext()) {
AttachmentPart attached = (AttachmentPart) iterator.next();
String id = attached.getContentId();
String type = attached.getContentType();
System.out.println(
"Attachment " + id + " has content type " + type);
if (type.equals("text/plain")) {
Object content = attached.getContent();
System.out.println("Attachment contains:\n" + content);
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.toString());
System.exit(1);
} catch (IOException e) {
System.out.println("I/O exception: " + e.toString());
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("array exception" +e.toString());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
package pack1;
import java.io.*;
import java.util.Iterator;
import javax.xml.soap.*;
public class attatch1 {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
String line = "";
try {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPBody body = message.getSOAPBody();
header.detachNode();
AttachmentPart attachment1 = message.createAttachmentPart();
fr = new FileReader(new File("rose1.txt"));
br = new BufferedReader(fr);
String stringContent = "";
line = br.readLine();
while (line != null) {
stringContent = stringContent.concat(line);
stringContent = stringContent.concat("\n");
line = br.readLine();
}
attachment1.setContent(stringContent, "text/plain");
attachment1.setContentId("attached_text");
message.addAttachmentPart(attachment1);
Iterator iterator = message.getAttachments();
while (iterator.hasNext()) {
AttachmentPart attached = (AttachmentPart) iterator.next();
String id = attached.getContentId();
String type = attached.getContentType();
System.out.println(
"Attachment " + id + " has content type " + type);
if (type.equals("text/plain")) {
Object content = attached.getContent();
System.out.println("Attachment contains:\n" + content);
}
}
}
catch (FileNotFoundException e) {
System.out.println("File not found: " + e.toString());
System.exit(1);
}
catch (IOException e) {
System.out.println("I/O exception: " + e.toString());
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("array exception" +e.toString());
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
Creating text attatchment file

Figure. 5
Hi
Welcome to the SAAJ Application
This is the Example of Attached Text Message.
Best Regards
RoseIndia

Figure 6
API used in the program
This file uses the file handling API such as FileReader and
BufferedReader and Web Service API as given below
|
Recommend the tutorial |

Ask Questions? Discuss: SOAP with Attachments API for Java
Post your Comment