【Struts2学习笔记(9)】单文件上传和多文件上传
来源:互联网 发布:电力数据通信网 编辑:程序博客网 时间:2024/05/17 00:13
(1)单文件上传
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。
这两个文件可以从http://commons.apache.org/下载。(如果是直接使用Add Struts capacities 则可以不用这几个jar包)
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post"> <input type="file" name="uploadImage"></form>
第三步:在Action类中添加以下属性:
package cn.lc.action;import java.io.File;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class HelloWorldAction {private File image;private String imageFileName;public String getImageFileName() {return imageFileName;}public void setImageFileName(String imageFileName) {this.imageFileName = imageFileName;}public File getImage() {return image;}public void setImage(File image) {this.image = image;}public String addUI() {return "success";}public String execute() throws Exception {// 获得路径String realpath = ServletActionContext.getServletContext().getRealPath("/images");System.out.println(realpath);//判断文件不为空的时候才保存if (image != null) {File savefile = new File(new File(realpath), imageFileName);//获得稳健目录 如果不存在则创建if (!savefile.getParentFile().exists()) {savefile.getParentFile().mkdirs();}FileUtils.copyFile(image, savefile);ActionContext.getContext().put("message", "上传成功");}return "success";}}
第四步:上边的代码在上擦混大文件的时候会出现错误这是因为默认的大小只有几兆 如果需要上传稍大一些的文件则需要修改struts.xml默认的文件大小:
<struts> <!-- 使用常量来设置上传文件的大小 大小为10兆左右--> <constant name="struts.multipart.maxSize" value="10701096"/> <package name="employee" namespace="/control/employee" extends="struts-default"><action name="list_*" class="cn.itcast.action.HelloWorldAction" method="{1}"><result name="success">/WEB-INF/page/message.jsp</result></action></package></struts>
(2)多文件上传
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post"> <input type="file" name="uploadImages"> <input type="file" name="uploadImages"></form>
第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:
package cn.lc.action;import java.io.File;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class HelloWorldAction {private File[] image;private String[] imageFileName;public File[] getImage() {return image;}public void setImage(File[] image) {this.image = image;}public String[] getImageFileName() {return imageFileName;}public void setImageFileName(String[] imageFileName) {this.imageFileName = imageFileName;}public String addUI() {return "success";}public String execute() throws Exception {String realpath = ServletActionContext.getServletContext().getRealPath("/images");System.out.println(realpath);if (image != null) {File savedir = new File(realpath);if (!savedir.exists())savedir.mkdirs();for (int i = 0; i < image.length; i++) {File savefile = new File(savedir, imageFileName[i]);FileUtils.copyFile(image[i], savefile);}ActionContext.getContext().put("message", "上传成功");}return "success";}}
同样的 也可以设置文件上传的大小限制!
注:转载请注明出处!
1 0
- 【Struts2学习笔记(9)】单文件上传和多文件上传
- Struts2.0学习笔记---上传文件和多文件上传
- struts2文件上传(单文件/多文件)
- Struts2 单文件上传
- struts2单文件上传
- struts2单文件上传
- Struts2 单文件上传
- Struts2 单文件上传
- Struts2单文件上传
- struts2单文件上传
- Struts2单文件上传
- struts2单文件和多文件的上传和下载
- Struts2学习笔记(七) 上传文件
- Struts2学习笔记(十二)文件上传
- struts2学习笔记(十一)文件上传
- 单文件上传和多文件上传
- struts2单文件和多文件的上传
- Struts2的多文件上传 和 单文件下载
- CreateNewXML
- 2024 C语言合法标识符
- window dos基本操作命令介绍
- 金九银十,浅谈关于java程序员面试的一些事项
- XMLParsing
- 【Struts2学习笔记(9)】单文件上传和多文件上传
- split()根据关键字符分割字符串 →_→还在码代码的马子耕←_←
- 神奇的Lua:可变参数
- NSAttributedString
- 搭建Hadoop环境----CentOs安装和配置(二)
- android驱动例子(LED灯控制)
- 善用指针---指针数组
- mysql中date与datetime的区别
- Unity 3D 文件导入出错