KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > AttachmentServlet


1 /*
2  * @(#)AttachmentServlet.java 1.3 02/12/20
3  *
4  * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  *
38  */

39
40 package demo;
41
42 import java.io.*;
43 import javax.mail.*;
44 import javax.mail.internet.*;
45 import javax.servlet.*;
46 import javax.servlet.http.*;
47
48 /**
49  * This servlet gets the input stream for a given msg part and
50  * pushes it out to the browser with the correct content type.
51  * Used to display attachments and relies on the browser's
52  * content handling capabilities.
53  */

54 public class AttachmentServlet extends HttpServlet {
55
56     /**
57      * This method handles the GET requests from the client.
58      */

59     public void doGet(HttpServletRequest request, HttpServletResponse response)
60         throws IOException, ServletException {
61       
62         HttpSession session = request.getSession();
63         ServletOutputStream out = response.getOutputStream();
64         int msgNum = Integer.parseInt(request.getParameter("message"));
65         int partNum = Integer.parseInt(request.getParameter("part"));
66         MailUserBean mailuser = (MailUserBean)session.getAttribute("mailuser");
67
68         // check to be sure we're still logged in
69
if (mailuser.isLoggedIn()) {
70             try {
71                 Message msg = mailuser.getFolder().getMessage(msgNum);
72
73                 Multipart multipart = (Multipart)msg.getContent();
74             Part part = multipart.getBodyPart(partNum);
75                 
76                 String JavaDoc sct = part.getContentType();
77             if (sct == null) {
78             out.println("invalid part");
79             return;
80             }
81             ContentType ct = new ContentType(sct);
82
83             response.setContentType(ct.getBaseType());
84             InputStream is = part.getInputStream();
85             int i;
86             while ((i = is.read()) != -1)
87             out.write(i);
88             out.flush();
89             out.close();
90
91             } catch (MessagingException ex) {
92                 throw new ServletException(ex.getMessage());
93             }
94         } else {
95             getServletConfig().getServletContext().
96                 getRequestDispatcher("/index.html").
97                 forward(request, response);
98         }
99     }
100 }
101
102
Popular Tags