1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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("<h1>Results</h1>");
66 out.println("<body>");
67 out.println("<p>");
68
69 Collection<Part> parts = request.getParts();
70 out.println("<b>Parts:</b> "+parts.size());
71 for (Part p: parts)
72 {
73 out.println("<h3>"+p.getName()+"</h3>");
74 out.println("<b>Size:</b> "+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 }