|
17 | 17 |
|
18 | 18 | package org.openqa.selenium.environment.webserver;
|
19 | 19 |
|
| 20 | +import org.seleniumhq.jetty9.server.Request; |
| 21 | + |
20 | 22 | import java.io.File;
|
21 | 23 | import java.io.FileInputStream;
|
22 | 24 | import java.io.IOException;
|
23 | 25 | import java.io.InputStream;
|
24 | 26 |
|
| 27 | +import javax.servlet.MultipartConfigElement; |
25 | 28 | import javax.servlet.ServletException;
|
26 | 29 | import javax.servlet.http.HttpServlet;
|
27 | 30 | import javax.servlet.http.HttpServletRequest;
|
28 | 31 | import javax.servlet.http.HttpServletResponse;
|
| 32 | +import javax.servlet.http.Part; |
29 | 33 |
|
30 | 34 | /**
|
31 | 35 | * A simple file upload servlet that just sends back the file contents to the client.
|
|
34 | 38 | */
|
35 | 39 | public class UploadServlet extends HttpServlet {
|
36 | 40 |
|
| 41 | + private static final MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement(System.getProperty("java.io.tmpdir")); |
| 42 | + |
37 | 43 | @Override
|
38 | 44 | protected void doPost(HttpServletRequest request,
|
39 | 45 | HttpServletResponse response)
|
40 | 46 | throws ServletException, IOException {
|
41 | 47 | response.setContentType("text/html");
|
42 | 48 | response.setStatus(HttpServletResponse.SC_OK);
|
43 | 49 |
|
44 |
| - File file = (File) request.getAttribute("upload"); |
45 |
| - file.deleteOnExit(); |
| 50 | + request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG); |
| 51 | + Part upload = request.getPart("upload"); |
46 | 52 |
|
47 |
| - int length = (int) file.length(); |
48 |
| - byte[] buffer = new byte[length]; |
| 53 | + byte[] buffer = new byte[(int) upload.getSize()]; |
49 | 54 | InputStream in = null;
|
50 | 55 | String content;
|
51 | 56 | try {
|
52 |
| - in = new FileInputStream(file); |
53 |
| - in.read(buffer, 0, length); |
| 57 | + in = upload.getInputStream(); |
| 58 | + in.read(buffer, 0, (int) upload.getSize()); |
54 | 59 | content = new String(buffer, "UTF-8");
|
55 | 60 | } finally {
|
56 | 61 | if (in != null) {
|
|
0 commit comments