package com.lovo.struts.action; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.upload.FormFile; import com.lovo.struts.form.UploadForm; public class UploadAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub UploadForm uf = (UploadForm) form; FormFile myFile = uf.getMyFile(); String path = servlet.getServletContext().getRealPath("/upload"); String fileName = myFile.getFileName(); InputStream in = null; OutputStream out = null; try { in = myFile.getInputStream(); out = new FileOutputStream(path + "/" + fileName); // int b = 0; // while ((b = in.read()) != -1) { // out.write(b); // } byte[] b = new byte[1024]; int length = 0; while((length=in.read(b)) != -1){ out.write(b,0,length); out.flush(); } ActionMessages messages = new ActionMessages(); messages.add("success", new ActionMessage("uploadSuccess")); this.addMessages(request, messages); } catch (IOException ioe) { } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } return mapping.findForward("success"); } } 上<bean:size id="allNum" name="allGirls" />//创建一个bean <bean:define id="newName" name="username"></bean:define> <bean:define id="girlAge" name="girl" property="age"></bean:define> <bean:define id="newBean" value="春哥万岁!"></bean:define>//定义的变量 <H1> This is my Success page. </H1> <br> 欢迎你: <bean:write name="newName" /> <br> 受害者是: <bean:write name="username" scope="session" />//在session作用域里获取username值 <br> 她叫: <bean:write name="girl" property="name" />获取girl值,property="name" 获得对象里name属性 今年都 <bean:write name="girlAge" /> 岁! <br> 受害者一共达到了 <bean:write name="allNum" />//打印出来多少数值 位! <br> <bean:write name="newBean"/>//打印 传:JSP页面: <html:form action="upload.do" enctype="multipart/form-data">//以二进制往上传 <html:file property="myFile"></html:file> <html:submit value="上传"></html:submit><br> </html:form> enctype="multipart/form-data" // JAVA: html bean: