FileUploadController.jpf Sample

This topic inludes the source code for the FileUploadController.jpf Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebApp/tagSamples/netui/fileUpload/

Sample Source Code


001 package tagSamples.netui.fileUpload;
002 import com.bea.wlw.netui.pageflow.FormData;
003 import com.bea.wlw.netui.pageflow.Forward;
004 import com.bea.wlw.netui.pageflow.PageFlowController;
005 import java.io.FileNotFoundException;
006 import java.io.IOException;
007 import java.io.InputStreamReader;
008 import java.io.StringWriter;
009 import org.apache.struts.upload.FormFile;
010 
011 /**
012  * @jpf:controller
013  * @jpf:view-properties view-properties::
014  <!-- This data is auto-generated. Hand-editing this section is not recommended. -->
015  <view-properties>
016  <pageflow-object id="pageflow:/tagSamples/netui/fileUpload/fileUploadController.jpf"/>
017  <pageflow-object id="action:begin.do">
018  *   <property value="80" name="x"/>
019  *   <property value="120" name="y"/>
020  </pageflow-object>
021  <pageflow-object id="action:uploadFile.do#tagSamples.netui.fileUpload.fileUploadController.UploadFileForm">
022  *   <property value="280" name="x"/>
023  *   <property value="240" name="y"/>
024  </pageflow-object>
025  <pageflow-object id="action:backToBrowsePage.do">
026  *   <property value="80" name="x"/>
027  *   <property value="400" name="y"/>
028  </pageflow-object>
029  <pageflow-object id="action-call:@page:browseForFile.jsp@#@action:uploadFile.do#tagSamples.netui.fileUpload.fileUploadController.UploadFileForm@">
030  *   <property value="116,180,180,244" name="elbowsX"/>
031  *   <property value="232,232,232,232" name="elbowsY"/>
032  *   <property value="East_1" name="fromPort"/>
033  *   <property value="West_1" name="toPort"/>
034  </pageflow-object>
035  <pageflow-object id="page:browseForFile.jsp">
036  *   <property value="80" name="x"/>
037  *   <property value="240" name="y"/>
038  </pageflow-object>
039  <pageflow-object id="action-call:@page:showFile.jsp@#@action:backToBrowsePage.do@">
040  *   <property value="244,180,180,116" name="elbowsX"/>
041  *   <property value="392,392,392,392" name="elbowsY"/>
042  *   <property value="West_1" name="fromPort"/>
043  *   <property value="East_1" name="toPort"/>
044  </pageflow-object>
045  <pageflow-object id="page:showFile.jsp">
046  *   <property value="280" name="x"/>
047  *   <property value="400" name="y"/>
048  </pageflow-object>
049  <pageflow-object id="forward:path#success#browseForFile.jsp#@action:begin.do@">
050  *   <property value="80,80,80,80" name="elbowsX"/>
051  *   <property value="164,180,180,196" name="elbowsY"/>
052  *   <property value="South_1" name="fromPort"/>
053  *   <property value="North_1" name="toPort"/>
054  *   <property value="success" name="label"/>
055  </pageflow-object>
056  <pageflow-object id="forward:path#success#showFile.jsp#@action:uploadFile.do#tagSamples.netui.fileUpload.fileUploadController.UploadFileForm@">
057  *   <property value="280,280,280,280" name="elbowsX"/>
058  *   <property value="284,320,320,356" name="elbowsY"/>
059  *   <property value="South_1" name="fromPort"/>
060  *   <property value="North_1" name="toPort"/>
061  *   <property value="success" name="label"/>
062  </pageflow-object>
063  <pageflow-object id="forward:path#success#browseForFile.jsp#@action:backToBrowsePage.do@">
064  *   <property value="80,80,80,80" name="elbowsX"/>
065  *   <property value="356,320,320,284" name="elbowsY"/>
066  *   <property value="North_1" name="fromPort"/>
067  *   <property value="South_1" name="toPort"/>
068  *   <property value="success" name="label"/>
069  </pageflow-object>
070  <pageflow-object id="formbeanprop:tagSamples.netui.fileUpload.fileUploadController.UploadFileForm#theFile#org.apache.struts.upload.FormFile"/>
071  <pageflow-object id="formbean:tagSamples.netui.fileUpload.fileUploadController.UploadFileForm"/>
072  </view-properties>
073  * ::
074  */
075 public class fileUploadController extends PageFlowController
076 {
077     
078     public String strFile;
079     
080     public String strFileName;
081 
082     /**
083      * @jpf:action
084      * @jpf:forward name="success" path="browseForFile.jsp"
085      */
086     protected Forward begin()
087     {
088         return new Forward"success" );
089     }
090 
091     /**
092      * This method converts the uploaded file to a String format,
093      * and fowards the user to a page where the String is displayed.
094      
095      * @jpf:action
096      * @jpf:forward name="success" path="showFile.jsp"
097      */
098     protected Forward uploadFile(UploadFileForm form)
099         throws FileNotFoundException, IOException
100     {
101         // Get the size of the file.
102         int filesize = (form.theFile.getFileSize());
103         
104         // Make a char Array big enough to fit the file. 
105         char[] charArr = new char[filesize]
106         
107         //Create an input stream to read the file.
108         InputStreamReader inr = new InputStreamReader(form.theFile.getInputStream())
109         
110         // Load the input stream into the char Array.
111         inr.read(charArr);
112         
113         //Close the input stream.
114         inr.close();
115         
116         // Make a String writer to hold uploaded file as a String.
117         StringWriter strwr = new StringWriter()
118         
119         // Write the char Array to the StringBuffer. 
120         // Each StringWriter has an internal StringBuffer object.
121         // You can access the StringBuffer with the .getBuffer method.
122         strwr.write(charArr);
123         
124         // Store the file name and the file content in strFileName and strFile, respectively.
125         // showFile.jsp databinds to these two String objects.
126         strFileName = form.theFile.getFileName();
127         strFile = strwr.getBuffer().toString()
128         
129         //Close the StringWriter object.
130         strwr.close();
131         
132         // Foward the user to the showFile.jsp page. 
133         return new Forward("success");
134     }
135 
136     /**
137      * @jpf:action
138      * @jpf:forward name="success" path="browseForFile.jsp"
139      */
140     protected Forward backToBrowsePage()
141     {
142         return new Forward("success");
143     }
144 
145     /**
146      * FormData get and set methods may be overwritten by the Form Bean editor.
147      */
148     public static class UploadFileForm extends FormData
149     {
150         private org.apache.struts.upload.FormFile theFile;
151 
152         public void setTheFile(org.apache.struts.upload.FormFile theFile)
153         {
154             this.theFile = theFile;
155         }
156 
157         public org.apache.struts.upload.FormFile getTheFile()
158         {
159             return this.theFile;
160         }
161     }
162 }