1
2
3
4
5 package com.ozacc.mail.fetch.impl.sk_jp;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.util.ArrayList;
11 import java.util.List;
12 import javax.mail.MessagingException;
13 import javax.mail.Part;
14 import javax.mail.internet.ContentType;
15
16 /***
17 * 添付ファイルを抽出するPartHandlerです。
18 * <p>
19 * MultipartUtility#process()呼び出し後にgetFileNames()によって、 添付ファイル名の配列を得ることができます。
20 * </p>
21 * <p>
22 * ファイル名配列のindexを指定してその添付ファイルに対する
23 * InputStreamを得たり、渡されたOutputStreamに対して書き出すことができます。
24 * </p>
25 * @version $Revision: 1.1.2.2 $ $Date: 2005/09/25 12:51:38 $
26 * @author Shin
27 */
28 public class AttachmentsExtractor implements PartHandler {
29
30 /*** message/*のパートを無視します。 */
31 public static final int MODE_IGNORE_MESSAGE = 1;
32
33 /*** Content-Disposition: inline; パートはfilenameがあっても無視します。 */
34 public static final int MODE_IGNORE_INLINE = 2;
35
36 private final int mode;
37
38 private final List attachmentParts = new ArrayList();
39
40 /***
41 * 添付ファイル一覧を得るためのPartHandlerを作成します。 message/*のパートやinline且つファイル名指定ありのパートも
42 * 添付ファイルとして扱います。
43 */
44 public AttachmentsExtractor() {
45 this(0);
46 }
47
48 /***
49 * 添付ファイル一覧を得るためのPartHandlerを作成します。
50 * @param mode 動作モード。MODE_で始まる識別子をor指定します。
51 */
52 public AttachmentsExtractor(int mode) {
53 this.mode = mode;
54 }
55
56 /*** MultipartUtility#process()から呼びだされるメソッドです。 */
57 public boolean processPart(Part part, ContentType context) throws MessagingException,
58 IOException {
59
60 if (part.getContentType().indexOf("application/applefile") != -1) {
61 return true;
62 }
63
64 if (part.isMimeType("message/*")) {
65 if ((mode & MODE_IGNORE_MESSAGE) != 0) {
66 return true;
67 }
68 attachmentParts.add(part);
69 return true;
70 }
71 if (MailUtility.getFileName(part) == null) {
72 return true;
73 }
74 if ((mode & MODE_IGNORE_INLINE) != 0 && Part.INLINE.equalsIgnoreCase(part.getDisposition())) {
75 return true;
76 }
77
78 attachmentParts.add(part);
79 return true;
80 }
81
82 /***
83 * 添付ファイル個数を返します。
84 */
85 public int getCount() {
86 return attachmentParts.size();
87 }
88
89 /***
90 * 添付ファイル名の配列を返します。
91 * <P>
92 * 添付ファイルが存在しない場合は空の配列を返します。 <BR>
93 * ファイル名は同一のものが複数存在する事もありえます。
94 * </P>
95 */
96 public String[] getFileNames() throws MessagingException {
97 String[] names = new String[getCount()];
98 for (int i = 0; i < names.length; i++) {
99 names[i] = getFileName(i);
100 }
101 return names;
102 }
103
104 /***
105 * 指定添付ファイルのファイル名を返します。
106 */
107 public String getFileName(int index) throws MessagingException {
108 Part part = (Part)attachmentParts.get(index);
109 String name = MailUtility.getFileName(part);
110 if (name == null) {
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public String getContentType(int index) throws MessagingException {
128 return MailUtility.unfold(((Part)attachmentParts.get(index)).getContentType());
129 }
130
131 /***
132 * 指定添付ファイルのサイズを返します。
133 */
134 public int getSize(int index) throws MessagingException {
135 return ((Part)attachmentParts.get(index)).getSize();
136 }
137
138 /***
139 * 指定添付ファイルを読み込むストリームを返します。
140 */
141 public InputStream getInputStream(int index) throws MessagingException, IOException {
142 return ((Part)attachmentParts.get(index)).getInputStream();
143 }
144
145 /***
146 * 指定添付ファイルを指定ストリームに書き出します。
147 */
148 public void writeTo(int index, OutputStream out) throws MessagingException, IOException {
149 InputStream in = getInputStream(index);
150 byte[] buf = new byte[1024];
151 int len;
152 while ((len = in.read(buf)) != -1) {
153 out.write(buf, 0, len);
154 }
155 }
156
157 public static void main(String[] args) throws Exception {
158 javax.mail.internet.MimeMessage msg = new javax.mail.internet.MimeMessage(
159 javax.mail.Session.getDefaultInstance(System.getProperties(), null), System.in);
160 AttachmentsExtractor h = new AttachmentsExtractor();
161 MultipartUtility.process(msg, h);
162 for (int i = 0; i < h.getCount(); i++) {
163 System.out.println("Attachment no : " + i);
164 System.out.println("Filename = " + h.getFileName(i));
165 System.out.println("******************");
166 h.writeTo(i, System.out);
167 }
168 }
169 }