KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mypackage > Attachments


1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28 package mypackage;
29
30 import javax.xml.soap.*;
31 import java.net.*;
32 import java.util.*;
33 import java.io.*;
34 import javax.activation.*;
35
36
37
38 public class Attachments {
39     public static void main(String JavaDoc[] args) {
40         FileReader fr = null;
41         BufferedReader br = null;
42         String JavaDoc line = "";
43
44         try {
45             // One argument is passed in from build.xml
46
if (args.length != 1) {
47                 System.err.println("Usage: asant run " +
48                     "-Dtext-file=<filename>");
49                 System.exit(1);
50             }
51
52             // Create message factory
53
MessageFactory messageFactory = MessageFactory.newInstance();
54
55             // Create a message
56

57             SOAPMessage message = messageFactory.createMessage();
58
59             // Get the SOAP header and body from the message
60
// and remove the header
61
SOAPHeader header = message.getSOAPHeader();
62             SOAPBody body = message.getSOAPBody();
63             header.detachNode();
64
65             // Create attachment part for text
66
AttachmentPart attachment1 = message.createAttachmentPart();
67
68             fr = new FileReader(new File(args[0]));
69             br = new BufferedReader(fr);
70
71             String JavaDoc stringContent = "";
72             line = br.readLine();
73
74             while (line != null) {
75                 stringContent = stringContent.concat(line);
76                 stringContent = stringContent.concat("\n");
77                 line = br.readLine();
78             }
79
80             attachment1.setContent(stringContent, "text/plain");
81             attachment1.setContentId("attached_text");
82
83             message.addAttachmentPart(attachment1);
84
85             // Create attachment part for image
86
URL url = new URL("file:///../xml-pic.jpg");
87             DataHandler dataHandler = new DataHandler(url);
88             AttachmentPart attachment2 =
89                 message.createAttachmentPart(dataHandler);
90             attachment2.setContentId("attached_image");
91
92             message.addAttachmentPart(attachment2);
93
94             // Now extract the attachments
95
Iterator iterator = message.getAttachments();
96
97             while (iterator.hasNext()) {
98                 AttachmentPart attached = (AttachmentPart) iterator.next();
99                 String JavaDoc id = attached.getContentId();
100                 String JavaDoc type = attached.getContentType();
101                 System.out.println("Attachment " + id + " has content type " +
102                     type);
103
104                 if (type.equals("text/plain")) {
105                     Object JavaDoc content = attached.getContent();
106                     System.out.println("Attachment " + "contains:\n" + content);
107                 }
108             }
109         } catch (FileNotFoundException e) {
110             System.out.println("File not found: " + e.toString());
111             System.exit(1);
112         } catch (IOException e) {
113             System.out.println("I/O exception: " + e.toString());
114             System.exit(1);
115         } catch (Exception JavaDoc ex) {
116             ex.printStackTrace();
117         }
118     }
119 }
120
Popular Tags