View Javadoc

1   package com.panogenesis.webapp.action;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.InputStream;
6   import java.io.OutputStream;
7   
8   import javax.servlet.http.HttpServletRequest;
9   import javax.servlet.http.HttpServletResponse;
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  
14  import com.panogenesis.Constants;
15  
16  import org.springframework.validation.BindException;
17  import org.springframework.web.multipart.MultipartHttpServletRequest;
18  import org.springframework.web.multipart.commons.CommonsMultipartFile;
19  import org.springframework.web.servlet.ModelAndView;
20  import org.springframework.web.servlet.view.RedirectView;
21  
22  /***
23   * Controller class to upload Files.
24   *
25   * <p>
26   * <a href="FileUploadFormController.java.html"><i>View Source</i></a>
27   * </p>
28   *
29   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
30   */
31  public class FileUploadController extends BaseFormController {
32      public ModelAndView processFormSubmission(HttpServletRequest request,
33                                                HttpServletResponse response,
34                                                Object command,
35                                                BindException errors)
36      throws Exception {
37          if (request.getParameter("cancel") != null) {
38              return new ModelAndView(new RedirectView("mainMenu.html"));
39          }
40  
41          return super.processFormSubmission(request, response, command, errors);
42      }
43  
44      public ModelAndView onSubmit(HttpServletRequest request,
45                                   HttpServletResponse response, Object command,
46                                   BindException errors)
47      throws Exception {
48          FileUpload fileUpload = (FileUpload) command;
49  
50          // validate a file was entered
51          if (fileUpload.getFile().length == 0) {
52              Object[] args = 
53                  new Object[] { getText("uploadForm.file", request.getLocale()) };
54              errors.rejectValue("file", "errors.required", args, "File");
55              
56              return showForm(request, response, errors);
57          }
58  
59          MultipartHttpServletRequest multipartRequest =
60              (MultipartHttpServletRequest) request;
61          CommonsMultipartFile file =
62              (CommonsMultipartFile) multipartRequest.getFile("file");
63  
64          // the directory to upload to
65          String uploadDir =
66              getServletContext().getRealPath("/resources") + "/" +
67              request.getRemoteUser() + "/";
68  
69          // Create the directory if it doesn't exist
70          File dirPath = new File(uploadDir);
71  
72          if (!dirPath.exists()) {
73              dirPath.mkdirs();
74          }
75  
76          //retrieve the file data
77          InputStream stream = file.getInputStream();
78  
79          //write the file to the file specified
80          OutputStream bos =
81              new FileOutputStream(uploadDir + file.getOriginalFilename());
82          int bytesRead = 0;
83          byte[] buffer = new byte[8192];
84  
85          while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
86              bos.write(buffer, 0, bytesRead);
87          }
88  
89          bos.close();
90  
91          //close the stream
92          stream.close();
93  
94          // place the data into the request for retrieval on next page
95          request.setAttribute("friendlyName", fileUpload.getName());
96          request.setAttribute("fileName", file.getOriginalFilename());
97          request.setAttribute("contentType", file.getContentType());
98          request.setAttribute("size", file.getSize() + " bytes");
99          request.setAttribute("location",
100                              dirPath.getAbsolutePath() + Constants.FILE_SEP +
101                              file.getOriginalFilename());
102 
103         String link =
104             request.getContextPath() + "/resources" + "/" +
105             request.getRemoteUser() + "/";
106 
107         request.setAttribute("link", link + file.getOriginalFilename());
108 
109         return new ModelAndView(getSuccessView());
110     }
111 }