标签:
No. |
方法 |
类型 |
描述 |
1 |
public byte[] getFileData() throws FileNotFoundException,IOException |
普通 |
取得上传文件大小 |
2 |
public InputStream getInputStream() throws FileNotFoundException,IOException |
普通 |
取得上传文件的输入流 |
3 |
public int getFileSize() |
普通 |
取得上传文件的大小 |
4 |
public String getFileName() |
普通 |
取得上传文件的名称 |
5 |
public String getContentType() |
普通 |
取得上传文件的类型 |
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib uri="http://www.mldn.cn/struts/bean" prefix="bean"%> <%@ taglib uri="http://www.mldn.cn/struts/html" prefix="html"%> <%@ taglib uri="http://www.mldn.cn/struts/logic" prefix="logic"%> <html:html lang="true"> <head> <title>文件上传</title> </head> <body> <html:form action="/ch17/upload.do" method="post" enctype="multipart/form-data"> 请选择要上传的文件;<html:file property="photo"/> <html:submit value="上传"></html:submit> </html:form> </body> </html:html>
package org.lxh.struts.form; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; public class UploadForm extends ActionForm { private FormFile photo ; // 接收上传文件 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { // 暂不验证 return null; } public void reset(ActionMapping mapping, HttpServletRequest request) { } public FormFile getPhoto() { return photo; } public void setPhoto(FormFile photo) { this.photo = photo; } }
public class UploadAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UploadForm uploadForm = (UploadForm) form; IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()); // 自动生成文件名 String fileName = its.getIPTimeRand() + "." + uploadForm.getPhoto().getFileName().split("\\.")[uploadForm .getPhoto().getFileName().split("\\.").length - 1];// 生成文件名 File outFile = new File(super.getServlet().getServletContext() .getRealPath("/") + "upload"+ File.separator + uploadForm.getPhoto().getFileName().split("\\.")); // 输出文件路径 try{ OutputStream output = new FileOutputStream(outFile) ; // 文件输出 byte data[] = new byte[1024] ; // 接收文件 int temp = 0 ; // 结束判断 while ((temp = uploadForm.getPhoto().getInputStream() .read(data, 0, 1024)) != -1) { // 分块保存 output.write(data) ; // 保存文件 } output.close() ; // 关闭输出 }catch(Exception e){ e.printStackTrace() ; // 错误输出 } return null; } }
<struts-config> <form-beans> <form-bean name="uploadForm" type="org.lxh.struts.form.UploadForm" /> </form-beans> <action-mappings> <action attribute="uploadForm" input="/upload.jsp" name="uploadForm" path="/upload" scope="request" type="org.lxh.struts.action.UploadAction" /> </action-mappings> </struts-config>
这样就可以很方便的完成文件上传操作。
<struts-config> <form-beans> <form-bean name="newsForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="title" type="java.lang.String"> </form-property> <form-property name="content" type="java.lang.String"> </form-property> </form-bean> </form-beans> <action-mappings> <action attribute="newsForm" input="/ch17/news.jsp" name="newsForm" path="/ch17/news" scope="request" type="org.lxh.struts.action.NewsAction" /> </action-mappings> </struts-config>
<%@ page language="java" pageEncoding="GBK"%> <%@ taglib uri="http://www.mldn.cn/struts/bean" prefix="bean"%> <%@ taglib uri="http://www.mldn.cn/struts/html" prefix="html"%> <%@ taglib uri="http://www.mldn.cn/struts/logic" prefix="logic"%> <html:html lang="true"> <head> <title>动态actionForm</title> </head> <body> <html:form action="/ch17/news.do" method="post"> 标题:<html:text property="title"/><br> 内容:<html:text property="content"/><br> <html:submit value="提交"/><html:reset value="重置"/> </html:form> </body> </html:html>
package org.lxh.struts.action; 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.DynaActionForm; public class NewsAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { DynaActionForm dynaForm = (DynaActionForm) form ; String title = dynaForm.getString("title") ; // 取得title输入内容 String content = dynaForm.getString("content") ; // 取得content输入内容 System.out.println("title --> " + title) ; // 输出title内容 System.out.println("content --> " + content); // 输出content内容 return null; } }
标签:
原文地址:http://www.cnblogs.com/oumyye/p/4344867.html