View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.test;
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("<HEAD><link rel=\"stylesheet\" type=\"text/css\"  href=\"stylesheet.css\"/></HEAD>");
66              out.println("<body>");
67              out.println("<h1>Results</h1>");
68              out.println("<p>");
69  
70              Collection<Part> parts = request.getParts();
71              out.println("<b>Parts:</b>&nbsp;"+parts.size());
72              for (Part p: parts)
73              {
74                  out.println("<h3>"+p.getName()+"</h3>");
75                  out.println("<b>Size:</b>&nbsp;"+p.getSize());
76                  if (p.getContentType() == null || p.getContentType().startsWith("text/plain"))
77                  {
78                      out.println("<p>");
79                      IO.copy(p.getInputStream(),out);
80                      out.println("</p>");
81                  }
82              } 
83              out.println("</body>");            
84              out.println("</html>");
85              out.flush();
86          }
87          catch (ServletException e)
88          {
89              throw e;
90          }
91          catch (Exception e)
92          {
93              throw new ServletException(e);
94          }
95      }
96  
97      /* ------------------------------------------------------------ */
98      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
99      {      
100         try
101         {
102             response.setContentType("text/html");
103             ServletOutputStream out = response.getOutputStream();
104             out.println("<html>");
105             out.println("<body>");
106             out.println("<h1>Use a POST Instead</h1>");
107             out.println("</body>");            
108             out.println("</html>");
109             out.flush();
110         }
111         catch (Exception e)
112         {
113             throw new ServletException(e);
114         }
115     }
116     
117 
118   
119    
120 }