View Javadoc

1   package org.appfuse.webapp.action;
2   
3   import org.appfuse.Constants;
4   import org.primefaces.model.UploadedFile;
5   
6   import javax.servlet.http.HttpServletRequest;
7   import java.io.*;
8   
9   public class FileUpload extends BasePage implements Serializable {
10      private static final long serialVersionUID = 6932775516007291334L;
11      private UploadedFile file;
12      private String name;
13  
14      public UploadedFile getFile() {
15          return file;
16      }
17  
18      public void setFile(UploadedFile file) {
19          this.file = file;
20      }
21  
22      public String getName() {
23          return name;
24      }
25  
26      public void setName(String name) {
27          this.name = name;
28      }
29  
30      public String upload() throws IOException {
31          HttpServletRequest request = getRequest();
32  
33          // write the file to the filesystem
34          // the directory to upload to
35          String uploadDir = getFacesContext().getExternalContext().getRealPath("/resources");
36          // The following seems to happen when running jetty:run
37          if (uploadDir == null) {
38              uploadDir = new File("src/main/webapp/resources").getAbsolutePath();
39          }
40          uploadDir += "/" + request.getRemoteUser() + "/";
41  
42          // Create the directory if it doesn't exist
43          File dirPath = new File(uploadDir);
44  
45          if (!dirPath.exists()) {
46              dirPath.mkdirs();
47          }
48  
49          //retrieve the file data
50          InputStream stream = file.getInputstream();
51          String filename = file.getFileName();
52  
53          // APF-946: Canoo Web Tests R_1702 sets full path as name instead of only file name
54          if (filename.contains("/")) {
55              filename = filename.substring(filename.lastIndexOf("/") + 1);
56          }
57  
58          // APF-758: Fix for Internet Explorer's shortcomings
59          if (filename.contains("\\")) {
60              int slash = filename.lastIndexOf("\\");
61              if (slash != -1) {
62                  filename = filename.substring(slash + 1);
63              }
64              // Windows doesn't like /'s either
65              int slash2 = filename.lastIndexOf("/");
66              if (slash2 != -1) {
67                  filename = filename.substring(slash2 + 1);
68              }
69              // In case the name is C:foo.txt
70              int slash3 = filename.lastIndexOf(":");
71              if (slash3 != -1) {
72                  filename = filename.substring(slash3 + 1);
73              }
74          }
75  
76          //write the file to the file specified
77          OutputStream bos = new FileOutputStream(uploadDir + filename);
78          int bytesRead;
79          byte[] buffer = new byte[8192];
80  
81          while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
82              bos.write(buffer, 0, bytesRead);
83          }
84  
85          bos.close();
86  
87          //close the stream
88          stream.close();
89  
90          // place the data into the request for retrieval on next page
91          request.setAttribute("friendlyName", name);
92          request.setAttribute("fileName", filename);
93          request.setAttribute("contentType", file.getContentType());
94          request.setAttribute("size", file.getSize() + " bytes");
95          request.setAttribute("location", dirPath.getAbsolutePath() + Constants.FILE_SEP + filename);
96  
97          String link = request.getContextPath() + "/resources" + "/" + request.getRemoteUser() + "/";
98          request.setAttribute("link", link + filename);
99  
100         return "success";
101     }
102 }