View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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.lang.reflect.Method;
23  import java.util.List;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collection;
27  
28  import javax.servlet.ServletConfig;
29  import javax.servlet.ServletException;
30  import javax.servlet.ServletOutputStream;
31  import javax.servlet.http.HttpServlet;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.http.Part;
35  import javax.servlet.annotation.MultipartConfig;
36  
37  import org.eclipse.jetty.util.IO;
38  /**
39   * MultiPartTest
40   * 
41   * Test Servlet 3.0 MultiPart Mime handling.
42   * 
43   *
44   */
45  
46  @MultipartConfig(location="foo/bar", maxFileSize=10240, maxRequestSize=-1, fileSizeThreshold=2048)
47  public class MultiPartTest extends HttpServlet 
48  {
49      private ServletConfig config;
50      
51      
52      public void init(ServletConfig config) throws ServletException
53      {
54          super.init(config);
55          this.config = config;
56      }
57  
58      
59      
60      /* ------------------------------------------------------------ */
61      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
62      {
63  
64          try
65          {
66              response.setContentType("text/html");
67              ServletOutputStream out = response.getOutputStream();
68              out.println("<html>");
69              out.println("<h1>Results</h1>");
70              out.println("<body>");
71              out.println("<p>");
72  
73              Collection<Part> parts = request.getParts();
74              out.println("<b>Parts:</b>&nbsp;"+parts.size());
75              for (Part p: parts)
76              {
77                  out.println("<h3>"+p.getName()+"</h3>");
78                  out.println("<b>Size:</b>&nbsp;"+p.getSize());
79                  if (p.getContentType() == null || p.getContentType().startsWith("text/plain"))
80                  {
81                      out.println("<p>");
82                      IO.copy(p.getInputStream(),out);
83                      out.println("</p>");
84                  }
85              } 
86              out.println("</body>");            
87              out.println("</html>");
88              out.flush();
89          }
90          catch (ServletException e)
91          {
92              throw e;
93          }
94          catch (Exception e)
95          {
96              throw new ServletException(e);
97          }
98      }
99  
100     /* ------------------------------------------------------------ */
101     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
102     {      
103         try
104         {
105             response.setContentType("text/html");
106             ServletOutputStream out = response.getOutputStream();
107             out.println("<html>");
108             out.println("<body>");
109             out.println("<h1>Use a POST Instead</h1>");
110             out.println("</body>");            
111             out.println("</html>");
112             out.flush();
113         }
114         catch (Exception e)
115         {
116             throw new ServletException(e);
117         }
118     }
119     
120 
121   
122    
123 }