View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package com.acme;
20  
21  import java.io.IOException;
22  import java.util.Collection;
23  
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletOutputStream;
27  import javax.servlet.annotation.MultipartConfig;
28  import javax.servlet.http.HttpServlet;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import javax.servlet.http.Part;
32  
33  import org.eclipse.jetty.util.IO;
34  /**
35   * MultiPartTest
36   * 
37   * Test Servlet 3.0 MultiPart Mime handling.
38   * 
39   *
40   */
41  
42  @MultipartConfig(location="foo/bar", maxFileSize=10240, maxRequestSize=-1, fileSizeThreshold=2048)
43  public class MultiPartTest extends HttpServlet 
44  {
45      private ServletConfig config;
46      
47      
48      public void init(ServletConfig config) throws ServletException
49      {
50          super.init(config);
51          this.config = config;
52      }
53  
54      
55      
56      /* ------------------------------------------------------------ */
57      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
58      {
59  
60          try
61          {
62              response.setContentType("text/html");
63              ServletOutputStream out = response.getOutputStream();
64              out.println("<html>");
65              out.println("<h1>Results</h1>");
66              out.println("<body>");
67              out.println("<p>");
68  
69              Collection<Part> parts = request.getParts();
70              out.println("<b>Parts:</b>&nbsp;"+parts.size());
71              for (Part p: parts)
72              {
73                  out.println("<h3>"+p.getName()+"</h3>");
74                  out.println("<b>Size:</b>&nbsp;"+p.getSize());
75                  if (p.getContentType() == null || p.getContentType().startsWith("text/plain"))
76                  {
77                      out.println("<p>");
78                      IO.copy(p.getInputStream(),out);
79                      out.println("</p>");
80                  }
81              } 
82              out.println("</body>");            
83              out.println("</html>");
84              out.flush();
85          }
86          catch (ServletException e)
87          {
88              throw e;
89          }
90          catch (Exception e)
91          {
92              throw new ServletException(e);
93          }
94      }
95  
96      /* ------------------------------------------------------------ */
97      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
98      {      
99          try
100         {
101             response.setContentType("text/html");
102             ServletOutputStream out = response.getOutputStream();
103             out.println("<html>");
104             out.println("<body>");
105             out.println("<h1>Use a POST Instead</h1>");
106             out.println("</body>");            
107             out.println("</html>");
108             out.flush();
109         }
110         catch (Exception e)
111         {
112             throw new ServletException(e);
113         }
114     }
115     
116 
117   
118    
119 }