KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.appfuse.webapp.action;
2
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.OutputStream JavaDoc;
7
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
11 import org.apache.struts.action.Action;
12 import org.apache.struts.action.ActionForm;
13 import org.apache.struts.action.ActionForward;
14 import org.apache.struts.action.ActionMapping;
15 import org.apache.struts.upload.FormFile;
16 import org.appfuse.Constants;
17 import org.appfuse.webapp.form.UploadForm;
18
19
20 /**
21  * This class handles the uploading of a resume (or any file) and writing it to
22  * the filesystem. Eventually, it will also add support for persisting the
23  * files information into the database.
24  *
25  * <p>
26  * <a HREF="UploadAction.java.htm"><i>View Source</i></a>
27  * </p>
28  *
29  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
30  *
31  * @struts.action name="uploadForm" path="/uploadFile" scope="request" validate="true" input="failure"
32  * @struts.action-set-property property="cancellable" value="true"
33  * @struts.action-forward name="failure" path="/WEB-INF/pages/uploadForm.jsp"
34  * @struts.action-forward name="success" path="/WEB-INF/pages/uploadDisplay.jsp"
35  */

36 public class UploadAction extends Action {
37     public ActionForward execute(ActionMapping mapping, ActionForm form,
38                                  HttpServletRequest JavaDoc request,
39                                  HttpServletResponse JavaDoc response)
40       throws Exception JavaDoc {
41         // Did the user click the cancel button?
42
if (isCancelled(request)) {
43             request.removeAttribute(mapping.getAttribute());
44             return (mapping.findForward("mainMenu"));
45         }
46
47         //this line is here for when the input page is upload-utf8.jsp,
48
//it sets the correct character encoding for the response
49
String JavaDoc encoding = request.getCharacterEncoding();
50
51         if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) {
52             response.setContentType("text/html; charset=utf-8");
53         }
54
55         UploadForm theForm = (UploadForm) form;
56
57         //retrieve the name
58
String JavaDoc name = theForm.getName();
59
60         //retrieve the file representation
61
FormFile file = theForm.getFile();
62
63         if (file == null) {
64             return mapping.findForward("failure");
65         }
66
67         //retrieve the file name
68
String JavaDoc fileName = file.getFileName();
69
70         //retrieve the content type
71
String JavaDoc contentType = file.getContentType();
72
73         //retrieve the file size
74
String JavaDoc size = (file.getFileSize() + " bytes");
75
76         String JavaDoc data = null;
77         String JavaDoc filePath = null;
78
79         // the directory to upload to
80
String JavaDoc uploadDir =
81             servlet.getServletContext().getRealPath("/resources") + "/"
82             + request.getRemoteUser() + "/";
83
84         //write the file to the file specified
85
File JavaDoc dirPath = new File JavaDoc(uploadDir);
86
87         if (!dirPath.exists()) {
88             dirPath.mkdirs();
89         }
90
91         //retrieve the file data
92
InputStream JavaDoc stream = file.getInputStream();
93
94         //write the file to the file specified
95
OutputStream JavaDoc bos = new FileOutputStream JavaDoc(uploadDir + fileName);
96         int bytesRead = 0;
97         byte[] buffer = new byte[8192];
98
99         while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
100             bos.write(buffer, 0, bytesRead);
101         }
102
103         bos.close();
104
105         filePath = dirPath.getAbsolutePath() + Constants.FILE_SEP + file.getFileName();
106
107         //close the stream
108
stream.close();
109
110         // place the data into the request for retrieval on next page
111
request.setAttribute("friendlyName", name);
112         request.setAttribute("fileName", fileName);
113         request.setAttribute("contentType", contentType);
114         request.setAttribute("size", size);
115         request.setAttribute("data", data);
116         request.setAttribute("filePath", filePath);
117
118         String JavaDoc url = request.getContextPath() + "/resources" + "/" +
119             request.getRemoteUser() + "/" + file.getFileName();
120         request.setAttribute("url", url);
121         
122         //destroy the temporary file created
123
file.destroy();
124
125         //return a forward to display.jsp
126
return mapping.findForward("success");
127     }
128 }
129
Popular Tags