1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
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> "+parts.size());
72 for (Part p: parts)
73 {
74 out.println("<h3>"+p.getName()+"</h3>");
75 out.println("<b>Size:</b> "+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 }