KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > action > FileUploadAction


1 package org.appfuse.webapp.action;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.OutputStream JavaDoc;
8
9 import org.appfuse.Constants;
10
11 import com.opensymphony.webwork.ServletActionContext;
12 import com.opensymphony.xwork.ValidationAware;
13
14 public class FileUploadAction extends BaseAction implements ValidationAware {
15     private static final long serialVersionUID = -9208910183310010569L;
16     private File JavaDoc file;
17     private String JavaDoc fileContentType;
18     private String JavaDoc fileFileName;
19     private String JavaDoc name;
20
21     public String JavaDoc execute() throws Exception JavaDoc {
22         if (this.cancel != null) {
23             return "cancel";
24         }
25         
26         if (file == null || file.length() > 2097152) {
27             addActionError(getText("maxLengthExceeded"));
28             return INPUT;
29         }
30
31         // the directory to upload to
32
String JavaDoc uploadDir =
33             ServletActionContext.getServletContext().getRealPath("/resources") +
34             "/" + getRequest().getRemoteUser() + "/";
35
36         // write the file to the file specified
37
File JavaDoc dirPath = new File JavaDoc(uploadDir);
38
39         if (!dirPath.exists()) {
40             dirPath.mkdirs();
41         }
42
43         //retrieve the file data
44
InputStream JavaDoc stream = new FileInputStream JavaDoc(file);
45
46         //write the file to the file specified
47
OutputStream JavaDoc bos = new FileOutputStream JavaDoc(uploadDir + fileFileName);
48         int bytesRead = 0;
49         byte[] buffer = new byte[8192];
50
51         while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
52             bos.write(buffer, 0, bytesRead);
53         }
54
55         bos.close();
56         stream.close();
57
58         
59         // place the data into the request for retrieval on next page
60
getRequest().setAttribute("location", dirPath.getAbsolutePath()
61                 + Constants.FILE_SEP + fileFileName);
62         
63         String JavaDoc link =
64             getRequest().getContextPath() + "/resources" + "/" +
65             getRequest().getRemoteUser() + "/";
66         
67         getRequest().setAttribute("link", link + fileFileName);
68         
69         return SUCCESS;
70     }
71
72     public String JavaDoc doDefault() {
73         return INPUT;
74     }
75
76     public void setFile(File JavaDoc file) {
77         this.file = file;
78     }
79
80     public void setFileContentType(String JavaDoc fileContentType) {
81         this.fileContentType = fileContentType;
82     }
83
84     public void setFileFileName(String JavaDoc fileFileName) {
85         this.fileFileName = fileFileName;
86     }
87
88     public void setName(String JavaDoc name) {
89         this.name = name;
90     }
91
92     public String JavaDoc getName() {
93         return name;
94     }
95
96     public File JavaDoc getFile() {
97         return file;
98     }
99
100     public String JavaDoc getFileContentType() {
101         return fileContentType;
102     }
103
104     public String JavaDoc getFileFileName() {
105         return fileFileName;
106     }
107 }
108
Popular Tags