1.建立upload.jsp %@ page language="java" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><!-- The most important part is to declare your form's enctype to be "multipart/form-data", and to have a form:file element that maps to your ActionForm's FormFile property -->
<html:form action="upload.do" enctype="multipart/form-data"> Please enter some text, just to demonstrate the handling of text elements as opposed to file elements:<br /> <html:text property="theText" /><br /><br />
Please select the file that you would like to upload:<br /> <html:file property="theFile" /><br /><br /> If you would rather write this file to another file, please check here: <html:checkbox property="writeFile" /><br /><br /> If you checked the box to write to a file, please specify the file path here:<br /> <html:text property="filePath" /><br /><br />
<html:submit /> </html:form> 2.建立UploadForm.javapackage org.apache.struts.webapp.upload; import org.apache.struts.upload.FormFile; import org.apache.struts.action.ActionForm;public class UploadForm extends ActionForm { /** * The value of the text the user has sent as form data */ protected String theText; /** * Whether or not to write to a file */ protected boolean writeFile; /** * The file that the user has uploaded */ protected FormFile theFile; /** * The file path to write to */ protected String filePath; /** * Retrieve the value of the text the user has sent as form data */ public String getTheText() { return theText; } /** * Set the value of the form data text */ public void setTheText(String theText) { this.theText = theText; } /** * Retrieve a representation of the file the user has uploaded */ public FormFile getTheFile() { return theFile; } /** * Set a representation of the file the user has uploaded */ public void setTheFile(FormFile theFile) { this.theFile = theFile; } /** * Set whether or not to write to a file */ public void setWriteFile(boolean writeFile) { this.writeFile = writeFile; } /** * Get whether or not to write to a file */ public boolean getWriteFile() { return writeFile; } /** * Set the path to write a file to */ public void setFilePath(String filePath) { this.filePath = filePath; } /** * Get the path to write a file to */ public String getFilePath() { return filePath; } public void reset() { writeFile = false; } }
3.建立UploadAction.java package org.apache.struts.webapp.upload; import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.FileOutputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.struts.upload.FormFile; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ForwardingActionForward; public class UploadAction extends Action { public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { if (form instanceof UploadForm) { UploadForm theForm = (UploadForm) form; //retrieve the text data String text = theForm.getTheText(); //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("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"); } //this shouldn't happen in this example return null; } } 4.建立display.jsp <%@ page language="java" %><b>The Text</b>: <%= request.getAttribute("text") %> <br /><b>The File name</b>: <%= request.getAttribute("fileName") %> <br /><b>The File content type</b>: <%= request.getAttribute("contentType") %> <br /><b>The File size</b>: <%= request.getAttribute("size") %> <br /><b>The File data</b>: <br /><hr /> <pre><%= request.getAttribute("data") %></pre> <hr />应用时自己灵活改变
有没有用commons-fileupload组件,上传文件的例子啊?
to HITZXL(编程要厚道) omg,我的代码和你一模一样,但是传给action的form是null,哭啊!谁知道是怎么回事啊!是不是struts的版本问题啊!多谢,多多谢!
struts-upload.war
%@ page language="java" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %><!--
The most important part is to declare your form's enctype to be "multipart/form-data",
and to have a form:file element that maps to your ActionForm's FormFile property
-->
<html:form action="upload.do" enctype="multipart/form-data"> Please enter some text, just to demonstrate the handling of text elements as opposed to file elements:<br />
<html:text property="theText" /><br /><br />
Please select the file that you would like to upload:<br />
<html:file property="theFile" /><br /><br /> If you would rather write this file to another file, please check here:
<html:checkbox property="writeFile" /><br /><br /> If you checked the box to write to a file, please specify the file path here:<br />
<html:text property="filePath" /><br /><br />
<html:submit />
</html:form>
2.建立UploadForm.javapackage org.apache.struts.webapp.upload;
import org.apache.struts.upload.FormFile;
import org.apache.struts.action.ActionForm;public class UploadForm extends ActionForm {
/**
* The value of the text the user has sent as form data
*/
protected String theText; /**
* Whether or not to write to a file
*/
protected boolean writeFile; /**
* The file that the user has uploaded
*/
protected FormFile theFile; /**
* The file path to write to
*/
protected String filePath; /**
* Retrieve the value of the text the user has sent as form data
*/
public String getTheText() {
return theText;
} /**
* Set the value of the form data text
*/
public void setTheText(String theText) {
this.theText = theText;
} /**
* Retrieve a representation of the file the user has uploaded
*/
public FormFile getTheFile() {
return theFile;
} /**
* Set a representation of the file the user has uploaded
*/
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
} /**
* Set whether or not to write to a file
*/
public void setWriteFile(boolean writeFile) {
this.writeFile = writeFile;
} /**
* Get whether or not to write to a file
*/
public boolean getWriteFile() {
return writeFile;
} /**
* Set the path to write a file to
*/
public void setFilePath(String filePath) {
this.filePath = filePath;
} /**
* Get the path to write a file to
*/
public String getFilePath() {
return filePath;
} public void reset() {
writeFile = false;
}
}
package org.apache.struts.webapp.upload;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.upload.FormFile;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ForwardingActionForward;
public class UploadAction extends Action { public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) { if (form instanceof UploadForm) { UploadForm theForm = (UploadForm) form; //retrieve the text data
String text = theForm.getTheText(); //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("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");
} //this shouldn't happen in this example
return null;
}
}
4.建立display.jsp
<%@ page language="java" %><b>The Text</b>: <%= request.getAttribute("text") %> <br /><b>The File name</b>: <%= request.getAttribute("fileName") %> <br /><b>The File content type</b>: <%= request.getAttribute("contentType") %> <br /><b>The File size</b>: <%= request.getAttribute("size") %> <br /><b>The File data</b>: <br /><hr />
<pre><%= request.getAttribute("data") %></pre>
<hr />应用时自己灵活改变
<html:form action="upload.do" enctype="multipart/form-data">
-----------------------------
但是如果真的一模一样,我就不知道原因了
还有就是在tomcat4.1下,好用,至于5.0似乎有些问题
看看Struts自己带的文件上传的例子:
struts-upload.war
上传的目录可以在struts-config.xml里设置