struts的上传——必须使用struts的html标签
2010-06-21 08:29
791 查看
页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>addChairCushion.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<h3>ChairCushion 添加页面</h3>
<hr color="red">
<html:form action="operateChairCushion.do?method=addChairCushion" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>chairCushionName:</td>
<td>
<html:text property="chairCushionName" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>material:</td>
<td>
<html:text property="material" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>filling:</td>
<td>
<html:text property="filling" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>style:</td>
<td>
<html:text property="style" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>productUse:</td>
<td>
<html:text property="productUse" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>shape:</td>
<td>
<html:text property="shape" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>productPictureFile:</td>
<td><html:file property="productPictureFile" size="30"></html:file></td>
<td></td>
</tr>
<tr>
<td colspan="3" align="center">
<html:submit value="添加value">添加</html:submit>
<html:reset value="重置value">重置</html:reset>
</td>
</tr>
</table>
</html:form>
</center>
</body>
</html>
action:
public class LoadAction extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse)throws Exception {
LoadForm loadForm = (LoadForm) actionForm;
FormFile ff=loadForm.getFile();
if(ff==null)
{
return actionMapping.findForward("success");
}
String fname=ff.getFileName();
String size=Integer.toString(ff.getFileSize())+"bytes";
InputStream in=ff.getInputStream();
OutputStream out=new FileOutputStream("/"+fname);
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=in.read(buffer,0,8192))!=-1)
{
out.write(buffer,0,bytesRead);
}
out.close();
in.close();
loadForm.setFname(fname);
loadForm.setSize(size);
ff.destroy();
return actionMapping.findForward("success");
}
}
有以下几点需要注意:
1: <html:file>必须嵌套在<html:form>标签中。
2:<html:form>标签的method的属性必须设为"post".
3:<html:form>标签的编码类型enctype属性必须为"multipart/form-data"。
4:<html:file>标签必须设为property属性,这个属性和ActionForm Bean中FormFile类型的属性对应。
5:<html:text property="" disabled="true"></html:text>后,form获取不到值
6:<html:text property="" readonly="true"></html:text>后,可以获取值,而且页面不能修改值
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>addChairCushion.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<h3>ChairCushion 添加页面</h3>
<hr color="red">
<html:form action="operateChairCushion.do?method=addChairCushion" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>chairCushionName:</td>
<td>
<html:text property="chairCushionName" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>material:</td>
<td>
<html:text property="material" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>filling:</td>
<td>
<html:text property="filling" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>style:</td>
<td>
<html:text property="style" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>productUse:</td>
<td>
<html:text property="productUse" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>shape:</td>
<td>
<html:text property="shape" size="30">
</html:text>
</td>
<td></td>
</tr>
<tr>
<td>productPictureFile:</td>
<td><html:file property="productPictureFile" size="30"></html:file></td>
<td></td>
</tr>
<tr>
<td colspan="3" align="center">
<html:submit value="添加value">添加</html:submit>
<html:reset value="重置value">重置</html:reset>
</td>
</tr>
</table>
</html:form>
</center>
</body>
</html>
action:
public class LoadAction extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse)throws Exception {
LoadForm loadForm = (LoadForm) actionForm;
FormFile ff=loadForm.getFile();
if(ff==null)
{
return actionMapping.findForward("success");
}
String fname=ff.getFileName();
String size=Integer.toString(ff.getFileSize())+"bytes";
InputStream in=ff.getInputStream();
OutputStream out=new FileOutputStream("/"+fname);
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=in.read(buffer,0,8192))!=-1)
{
out.write(buffer,0,bytesRead);
}
out.close();
in.close();
loadForm.setFname(fname);
loadForm.setSize(size);
ff.destroy();
return actionMapping.findForward("success");
}
}
有以下几点需要注意:
1: <html:file>必须嵌套在<html:form>标签中。
2:<html:form>标签的method的属性必须设为"post".
3:<html:form>标签的编码类型enctype属性必须为"multipart/form-data"。
4:<html:file>标签必须设为property属性,这个属性和ActionForm Bean中FormFile类型的属性对应。
5:<html:text property="" disabled="true"></html:text>后,form获取不到值
6:<html:text property="" readonly="true"></html:text>后,可以获取值,而且页面不能修改值
相关文章推荐
- 英文操作系统使用struts中的formfile上传文件出现乱码
- Struts 2读书笔记-----使用Struts 2控制文件上传
- Struts学习总结(五)---使用Struts2实现单个文件上传
- iOS[正解] - 您的 App 正在使用广告标识符 (IDFA)。您必须先提供关于 IDFA 的使用信息或将其从 App 中移除,然后再上传您的二进制文件。
- struts1文件上传和下载 连接 使用Struts1实现文件上传(二)
- Struts中使用commons-fileupload实现上传例子
- struts2.X心得9--struts2自定义拦截器以及文件上传等几个拦截器使用案例
- Struts2国际化、拦截器配置以及使用struts上传文件
- struts使用总结一:慎用actionServlet成员变量,actionServlet生命周期,actionForm生命周期,struts上传注意事项
- 使用Struts上传文件,用FormFile取得上传文件的本地绝对路径
- CKEditor图片上传实现详细步骤(使用Struts 2)
- CKEditor图片上传实现详细步骤(使用Struts 2)
- 使用struts相关组件上传附件(org.apache.commons.fileupload.*)上传附件--详解
- PHP版本高于5.5时,curl文件上传必须使用CurlFile对象
- 使用Struts进行文件上传
- ssh框架 使用struts上传图片
- 使用Eclipse 3.1 WST编写Struts文件上传
- struts使用html:file上传文件的时候文件名乱码解决
- wangEditor使用Struts上传图片
- CKEditor图片上传实现详细步骤(使用Struts 2)