11package com .baeldung .servlets ;
22
3- import static com .baeldung .servlets .Constants .MAX_FILE_SIZE ;
4- import static com .baeldung .servlets .Constants .MAX_REQUEST_SIZE ;
5- import static com .baeldung .servlets .Constants .MEMORY_THRESHOLD ;
6- import static com .baeldung .servlets .Constants .UPLOAD_DIRECTORY ;
3+ import org .apache .commons .fileupload .FileItem ;
4+ import org .apache .commons .fileupload .disk .DiskFileItemFactory ;
5+ import org .apache .commons .fileupload .servlet .ServletFileUpload ;
76
8- import java .io .File ;
9- import java .io .IOException ;
10- import java .util .List ;
117import javax .servlet .ServletException ;
128import javax .servlet .http .HttpServlet ;
139import javax .servlet .http .HttpServletRequest ;
1410import javax .servlet .http .HttpServletResponse ;
15- import org .apache .commons .fileupload .FileItem ;
16- import org .apache .commons .fileupload .disk .DiskFileItemFactory ;
17- import org .apache .commons .fileupload .servlet .ServletFileUpload ;
11+ import java .io .File ;
12+ import java .io .IOException ;
13+ import java .util .List ;
14+
15+ import static com .baeldung .Constants .*;
1816
1917public class UploadServlet extends HttpServlet {
20-
21- private static final long serialVersionUID = 1L ;
22-
23- protected void doPost (HttpServletRequest request , HttpServletResponse response )
24- throws ServletException , IOException {
25-
26- if (ServletFileUpload .isMultipartContent (request )) {
27-
28- DiskFileItemFactory factory = new DiskFileItemFactory ();
29- factory .setSizeThreshold (MEMORY_THRESHOLD );
30- factory .setRepository (new File (System .getProperty ("java.io.tmpdir" )));
31-
32- ServletFileUpload upload = new ServletFileUpload (factory );
33- upload .setFileSizeMax (MAX_FILE_SIZE );
34- upload .setSizeMax (MAX_REQUEST_SIZE );
35- String uploadPath = getServletContext ().getRealPath ("" ) + File .separator + UPLOAD_DIRECTORY ;
36- File uploadDir = new File (uploadPath );
37- if (!uploadDir .exists ()) {
38- uploadDir .mkdir ();
39- }
40-
41- try {
42- List <FileItem > formItems = upload .parseRequest (request );
43-
44- if (formItems != null && formItems .size () > 0 ) {
45- for (FileItem item : formItems ) {
46- if (!item .isFormField ()) {
47- String fileName = new File (item .getName ()).getName ();
48- String filePath = uploadPath + File .separator + fileName ;
49- File storeFile = new File (filePath );
50- item .write (storeFile );
51- request .setAttribute ("message" , "File " + fileName + " has uploaded successfully!" );
52- }
53- }
54- }
55- } catch (Exception ex ) {
56- request .setAttribute ("message" ,"There was an error: " + ex .getMessage ());
57- }
58- getServletContext ().getRequestDispatcher ("/result.jsp" ).forward (request , response );
59- }
60- }
18+
19+ private static final long serialVersionUID = 1L ;
20+
21+ protected void doPost (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
22+
23+ if (ServletFileUpload .isMultipartContent (request )) {
24+
25+ DiskFileItemFactory factory = new DiskFileItemFactory ();
26+ factory .setSizeThreshold (MEMORY_THRESHOLD );
27+ factory .setRepository (new File (System .getProperty ("java.io.tmpdir" )));
28+
29+ ServletFileUpload upload = new ServletFileUpload (factory );
30+ upload .setFileSizeMax (MAX_FILE_SIZE );
31+ upload .setSizeMax (MAX_REQUEST_SIZE );
32+ String uploadPath = getServletContext ().getRealPath ("" ) + File .separator + UPLOAD_DIRECTORY ;
33+ File uploadDir = new File (uploadPath );
34+ if (!uploadDir .exists ()) {
35+ uploadDir .mkdir ();
36+ }
37+
38+ try {
39+ List <FileItem > formItems = upload .parseRequest (request );
40+
41+ if (formItems != null && formItems .size () > 0 ) {
42+ for (FileItem item : formItems ) {
43+ if (!item .isFormField ()) {
44+ String fileName = new File (item .getName ()).getName ();
45+ String filePath = uploadPath + File .separator + fileName ;
46+ File storeFile = new File (filePath );
47+ item .write (storeFile );
48+ request .setAttribute ("message" , "File " + fileName + " has uploaded successfully!" );
49+ }
50+ }
51+ }
52+ } catch (Exception ex ) {
53+ request .setAttribute ("message" , "There was an error: " + ex .getMessage ());
54+ }
55+ getServletContext ().getRequestDispatcher ("/result.jsp" ).forward (request , response );
56+ }
57+ }
6158}
0 commit comments