//retrieve the query string value
String queryValue = theForm.getQueryParam();
//retrieve the file representation
FormFile file = theForm.getTheFile();
//retrieve the file name
String fileName= file.getFileName();
//retrieve the content type
String contentType = file.getContentType();
boolean writeFile = theForm.getWriteFile();
//retrieve the file size
String size = (file.getFileSize() + " bytes");
String data = null;
try {
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
if (!writeFile) {
//only write files out that are less than 1MB
if (file.getFileSize() < (4*1024000)) {
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = new String(baos.toByteArray());
}
else {
data = new String("The file is greater than 4MB, " +
" and has not been written to stream." +
" File Size: " + file.getFileSize() + " bytes. This is a" +
" limitation of this particular web application, hard-coded" +
" in org.apache.struts.webapp.upload.UploadAction");
}
}
else {
//write the file to the file specified
OutputStream bos = new FileOutputStream(theForm.getFilePath());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
data = "The file has been written to \"" + theForm.getFilePath() + "\"";
}
//close the stream
stream.close();
}
catch (FileNotFoundException fnfe) {
return null;
}
catch (IOException ioe) {
return null;
}
//place the data into the request for retrieval from display.jsp
request.setAttribute("text", text);
request.setAttribute("queryValue", queryValue);
request.setAttribute("fileName", fileName);
request.setAttribute("contentType", contentType);
request.setAttribute("size", size);
request.setAttribute("data", data);
//destroy the temporary file created
file.destroy();
//return a forward to display.jsp
return mapping.findForward("display");
}