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.1 $ $Date: 2005/01/18 07:20:59 $
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 if (part.isMimeType("message/*")) {
60 if ((mode & MODE_IGNORE_MESSAGE) != 0) {
61 return true;
62 }
63 attachmentParts.add(part);
64 return true;
65 }
66 if (MailUtility.getFileName(part) == null) {
67 return true;
68 }
69 if ((mode & MODE_IGNORE_INLINE) != 0 && Part.INLINE.equalsIgnoreCase(part.getDisposition())) {
70 return true;
71 }
72 attachmentParts.add(part);
73 return true;
74 }
75
76 /***
77 * 添付ファイル個数を返します。
78 */
79 public int getCount() {
80 return attachmentParts.size();
81 }
82
83 /***
84 * 添付ファイル名の配列を返します。
85 * <P>
86 * 添付ファイルが存在しない場合は空の配列を返します。 <BR>
87 * ファイル名は同一のものが複数存在する事もありえます。
88 * </P>
89 */
90 public String[] getFileNames() throws MessagingException {
91 String[] names = new String[getCount()];
92 for (int i = 0; i < names.length; i++) {
93 names[i] = getFileName(i);
94 }
95 return names;
96 }
97
98 /***
99 * 指定添付ファイルのファイル名を返します。
100 */
101 public String getFileName(int index) throws MessagingException {
102 Part part = (Part)attachmentParts.get(index);
103 String name = MailUtility.getFileName(part);
104 if (name == null) {
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public String getContentType(int index) throws MessagingException {
122 return MailUtility.unfold(((Part)attachmentParts.get(index)).getContentType());
123 }
124
125 /***
126 * 指定添付ファイルのサイズを返します。
127 */
128 public int getSize(int index) throws MessagingException {
129 return ((Part)attachmentParts.get(index)).getSize();
130 }
131
132 /***
133 * 指定添付ファイルを読み込むストリームを返します。
134 */
135 public InputStream getInputStream(int index) throws MessagingException, IOException {
136 return ((Part)attachmentParts.get(index)).getInputStream();
137 }
138
139 /***
140 * 指定添付ファイルを指定ストリームに書き出します。
141 */
142 public void writeTo(int index, OutputStream out) throws MessagingException, IOException {
143 InputStream in = getInputStream(index);
144 byte[] buf = new byte[1024];
145 int len;
146 while ((len = in.read(buf)) != -1) {
147 out.write(buf, 0, len);
148 }
149 }
150
151 public static void main(String[] args) throws Exception {
152 javax.mail.internet.MimeMessage msg = new javax.mail.internet.MimeMessage(
153 javax.mail.Session.getDefaultInstance(System.getProperties(), null), System.in);
154 AttachmentsExtractor h = new AttachmentsExtractor();
155 MultipartUtility.process(msg, h);
156 for (int i = 0; i < h.getCount(); i++) {
157 System.out.println("Attachment no : " + i);
158 System.out.println("Filename = " + h.getFileName(i));
159 System.out.println("******************");
160 h.writeTo(i, System.out);
161 }
162 }
163 }