【Struts2学习笔记(9)】单文件上传和多文件上传
(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";
}
}
相同的 也能够设置文件上传的限制大小!
注:转载请注明出处!
【Struts2学习笔记(9)】单文件上传和多文件上传的更多相关文章
- Struts2学习笔记⑧
今天是Struts2学习笔记的最后一篇文章了.用什么做结尾呢,这两天其实还学了很多东西,没有记录下,今天就查漏补缺一下. 文件上传与下载.FreeMarker以及昨天没做完的例子 文件上传与下载 文件 ...
- Struts2 学习笔记(概述)
Struts2 学习笔记 2015年3月7日11:02:55 MVC思想 Strust2的MVC对应关系如下: 在MVC三个模块当中,struts2对应关系如下: Model: 负责封装应用的状态,并 ...
- Struts2学习笔记①
Struts2 学习笔记① 所有的程序学习都从Hello World开始,今天先跟着书做一个HW的示例. Struts2是一套MVC框架,使用起来非常方便,接触到现在觉得最麻烦的地方是配置文件.我的一 ...
- Struts2学习笔记NO.1------结合Hibernate完成查询商品类别简单案例(工具IDEA)
Struts2学习笔记一结合Hibernate完成查询商品类别简单案例(工具IDEA) 1.jar包准备 Hibernate+Struts2 jar包 struts的jar比较多,可以从Struts官 ...
- SQL反模式学习笔记12 存储图片或其他多媒体大文件
目标:存储图片或其他多媒体大文件 反模式:图片存储在数据库外的文件系统中,数据库表中存储文件的对应的路径和名称. 缺点: 1.文件不支持Delete操作.使用SQL语句删除一条记录时,对应的文 ...
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...
- Java学习笔记之---单例模型
Java学习笔记之---单例模型 单例模型分为:饿汉式,懒汉式 (一)要点 1.某个类只能有一个实例 2.必须自行创建实例 3.必须自行向整个系统提供这个实例 (二)实现 1.只提供私有的构造方法 2 ...
- Struts2学习笔记(十一)——文件上传
1.单文件上传 单文件上传步骤: 1)创建上传jsp页面 文件上传的表单提交方式必须是POST方式,编码类型:enctype="multipart/form-data",默认是 a ...
- 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
作者:ssslinppp 1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...
随机推荐
- java中write(byte[] b)与write(byte[] b,int off,int len)区别
public static void copyInputStreamT0OutputStream(InputStream in, OutputStream out) { byte[] buffer = ...
- paypal对接
paypal支付接口准备工作 首先去申请一个paypal账号,https://www.paypal.com/. 申请完毕并登录,进入https://developer.paypal.com/devel ...
- jquery常用写法简单记录
好久不写东西了......话不多说,主要记录一下,最近做的项目中用到的js的记录(虽然特别特别简单) 一 jquery常用写法记录 jQuery(this).addClass("select ...
- 全栈project师体能备战--知识面(1--10)
javascript 单例设计模式: 单例模式确保某个类仅仅有一个势力,并且自行实例化并向整个系统提供这个实例.如:cocos2dx中的导演类.[样例]我有6哥美丽的老婆,他们的老公都 ...
- DeJaVu update history
17.05.08 <-> Added Audi RB8 random code direct change -> Now can adapt VIN based keys or ke ...
- 从 n 个数字中选出 m 个不同的数字,保证这 m 个数字是等概率的
问题如上. 这是我被面试的一个题目. 我的第一反应给出的解决的方法是.开启 n 个线程并标记序号,各个线程打印出它的序号.直到有 m 个线程被调度时,停止全部线程. 打印出的序号即是 m 个等概率出 ...
- win8.1快速启动选项突然消失了怎么办?
win8开始提供的快速启动功能是一种混合式的休眠模式,Windows系统 在关机时将系统的信息保存到硬盘上的一个文件中来实现下一次的快速启动.当再次启动电脑时, Windows 使用该系统信息文件来恢 ...
- NodeJS学习资料合集
1. 官网 nodejs 2. How do I get started with Node.js,stackoverflow提问,收集非常多实用的网站 3. node-books.github收 ...
- 单点登录 之 OAuth
OAuth2.0是什么 OAuth2.0是什么——豆瓣和QQ的故事 OAuth简单说就是一种授权的协议,只要授权方和被授权方遵守这个协议去写代码提供服务,那双方就是实现了OAuth模式. 举个例子,你 ...
- [翻译] Working with NSURLSession: AFNetworking 2.0
Working with NSURLSession: AFNetworking 2.0 简单翻译,有很多错误,看官无法理解处请英文原文对照. http://code.tutsplus.com/tu ...