001: /*
002: * $Id: UploadAction.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.webapp.upload;
023:
024: import java.io.ByteArrayOutputStream;
025: import java.io.FileNotFoundException;
026: import java.io.FileOutputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.OutputStream;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.apache.struts.action.Action;
035: import org.apache.struts.action.ActionForm;
036: import org.apache.struts.action.ActionForward;
037: import org.apache.struts.action.ActionMapping;
038: import org.apache.struts.upload.FormFile;
039:
040: /**
041: * This class takes the UploadForm and retrieves the text value
042: * and file attributes and puts them in the request for the display.jsp
043: * page to display them
044: *
045: * @author Mike Schachter
046: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
047: */
048:
049: public class UploadAction extends Action {
050: public ActionForward execute(ActionMapping mapping,
051: ActionForm form, HttpServletRequest request,
052: HttpServletResponse response) throws Exception {
053:
054: if (form instanceof UploadForm) {
055:
056: //this line is here for when the input page is upload-utf8.jsp,
057: //it sets the correct character encoding for the response
058: String encoding = request.getCharacterEncoding();
059: if ((encoding != null)
060: && (encoding.equalsIgnoreCase("utf-8"))) {
061: response.setContentType("text/html; charset=utf-8");
062: }
063:
064: UploadForm theForm = (UploadForm) form;
065:
066: //retrieve the text data
067: String text = theForm.getTheText();
068:
069: //retrieve the query string value
070: String queryValue = theForm.getQueryParam();
071:
072: //retrieve the file representation
073: FormFile file = theForm.getTheFile();
074:
075: //retrieve the file name
076: String fileName = file.getFileName();
077:
078: //retrieve the content type
079: String contentType = file.getContentType();
080:
081: boolean writeFile = theForm.getWriteFile();
082:
083: //retrieve the file size
084: String size = (file.getFileSize() + " bytes");
085:
086: String data = null;
087:
088: try {
089: //retrieve the file data
090: ByteArrayOutputStream baos = new ByteArrayOutputStream();
091: InputStream stream = file.getInputStream();
092: if (!writeFile) {
093: //only write files out that are less than 1MB
094: if (file.getFileSize() < (4 * 1024000)) {
095:
096: byte[] buffer = new byte[8192];
097: int bytesRead = 0;
098: while ((bytesRead = stream
099: .read(buffer, 0, 8192)) != -1) {
100: baos.write(buffer, 0, bytesRead);
101: }
102: data = new String(baos.toByteArray());
103: } else {
104: data = new String(
105: "The file is greater than 4MB, "
106: + " and has not been written to stream."
107: + " File Size: "
108: + file.getFileSize()
109: + " bytes. This is a"
110: + " limitation of this particular web application, hard-coded"
111: + " in org.apache.struts.webapp.upload.UploadAction");
112: }
113: } else {
114: //write the file to the file specified
115: OutputStream bos = new FileOutputStream(theForm
116: .getFilePath());
117: int bytesRead = 0;
118: byte[] buffer = new byte[8192];
119: while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
120: bos.write(buffer, 0, bytesRead);
121: }
122: bos.close();
123: data = "The file has been written to \""
124: + theForm.getFilePath() + "\"";
125: }
126: //close the stream
127: stream.close();
128: } catch (FileNotFoundException fnfe) {
129: return null;
130: } catch (IOException ioe) {
131: return null;
132: }
133:
134: //place the data into the request for retrieval from display.jsp
135: request.setAttribute("text", text);
136: request.setAttribute("queryValue", queryValue);
137: request.setAttribute("fileName", fileName);
138: request.setAttribute("contentType", contentType);
139: request.setAttribute("size", size);
140: request.setAttribute("data", data);
141:
142: //destroy the temporary file created
143: file.destroy();
144:
145: //return a forward to display.jsp
146: return mapping.findForward("display");
147: }
148:
149: //this shouldn't happen in this example
150: return null;
151: }
152: }
|