xref: /unit/test/java/multipart/app.java (revision 1117:36151e7d24d6)
1 
2 import java.io.IOException;
3 import java.io.PrintWriter;
4 
5 import java.util.Map;
6 
7 import javax.servlet.ServletConfig;
8 import javax.servlet.ServletException;
9 
10 import javax.servlet.annotation.WebServlet;
11 import javax.servlet.annotation.MultipartConfig;
12 
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.io.InputStream;
16 import java.io.FileOutputStream;
17 import java.io.OutputStream;
18 import javax.servlet.http.Part;
19 
20 import javax.servlet.http.HttpServlet;
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23 
24 @WebServlet("/")
25 @MultipartConfig(
26         fileSizeThreshold   = 1024 * 1024 * 1,  // 1 MB
27         maxFileSize         = 1024 * 1024 * 10, // 10 MB
28         maxRequestSize      = 1024 * 1024 * 15 // 15 MB
29 )
30 public class app extends HttpServlet
31 {
32     @Override
doPost(HttpServletRequest request, HttpServletResponse response)33     public void doPost(HttpServletRequest request, HttpServletResponse response)
34         throws IOException, ServletException
35     {
36         response.setContentType("text/html;charset=UTF-8");
37 
38         // Create path components to save the file
39         final String path = request.getParameter("destination");
40         final Part filePart = request.getPart("file");
41         final String fileName = getFileName(filePart);
42 
43         OutputStream out = null;
44         InputStream filecontent = null;
45         final PrintWriter writer = response.getWriter();
46 
47         try {
48             out = new FileOutputStream(new File(path + File.separator
49                                                 + fileName));
50             filecontent = filePart.getInputStream();
51 
52             int read = 0;
53             final byte[] bytes = new byte[1024];
54 
55             while ((read = filecontent.read(bytes)) != -1) {
56                 out.write(bytes, 0, read);
57             }
58             writer.println(fileName + " created at " + path);
59 
60         } catch (FileNotFoundException fne) {
61             writer.println("You either did not specify a file to upload or are "
62                             + "trying to upload a file to a protected or nonexistent "
63                             + "location.");
64             writer.println("<br/> ERROR: " + fne.getMessage());
65 
66         } finally {
67             if (out != null) {
68                 out.close();
69             }
70             if (filecontent != null) {
71                 filecontent.close();
72             }
73             if (writer != null) {
74                 writer.close();
75             }
76         }
77 
78         return;
79     }
80 
getFileName(final Part part)81     private String getFileName(final Part part) {
82         final String partHeader = part.getHeader("content-disposition");
83 
84         for (String content : part.getHeader("content-disposition").split(";"))
85         {
86             if (content.trim().startsWith("filename")) {
87                 return content.substring(
88                     content.indexOf("=") + 1).trim().replace("\"", "");
89             }
90         }
91         return null;
92     }
93 }
94