(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)】单文件上传和多文件上传的更多相关文章

  1. Struts2学习笔记⑧

    今天是Struts2学习笔记的最后一篇文章了.用什么做结尾呢,这两天其实还学了很多东西,没有记录下,今天就查漏补缺一下. 文件上传与下载.FreeMarker以及昨天没做完的例子 文件上传与下载 文件 ...

  2. Struts2 学习笔记(概述)

    Struts2 学习笔记 2015年3月7日11:02:55 MVC思想 Strust2的MVC对应关系如下: 在MVC三个模块当中,struts2对应关系如下: Model: 负责封装应用的状态,并 ...

  3. Struts2学习笔记①

    Struts2 学习笔记① 所有的程序学习都从Hello World开始,今天先跟着书做一个HW的示例. Struts2是一套MVC框架,使用起来非常方便,接触到现在觉得最麻烦的地方是配置文件.我的一 ...

  4. Struts2学习笔记NO.1------结合Hibernate完成查询商品类别简单案例(工具IDEA)

    Struts2学习笔记一结合Hibernate完成查询商品类别简单案例(工具IDEA) 1.jar包准备 Hibernate+Struts2 jar包 struts的jar比较多,可以从Struts官 ...

  5. SQL反模式学习笔记12 存储图片或其他多媒体大文件

    目标:存储图片或其他多媒体大文件 反模式:图片存储在数据库外的文件系统中,数据库表中存储文件的对应的路径和名称. 缺点:     1.文件不支持Delete操作.使用SQL语句删除一条记录时,对应的文 ...

  6. python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码

    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...

  7. Java学习笔记之---单例模型

    Java学习笔记之---单例模型 单例模型分为:饿汉式,懒汉式 (一)要点 1.某个类只能有一个实例 2.必须自行创建实例 3.必须自行向整个系统提供这个实例 (二)实现 1.只提供私有的构造方法 2 ...

  8. Struts2学习笔记(十一)——文件上传

    1.单文件上传 单文件上传步骤: 1)创建上传jsp页面 文件上传的表单提交方式必须是POST方式,编码类型:enctype="multipart/form-data",默认是 a ...

  9. 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

    作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...

随机推荐

  1. 【CF148D】 Bag of mice (概率DP)

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Javascript 继承-原型的陷阱

    注:本文为翻译文章,原文为"JavaScript Inheritance – How To Shoot Yourself In the Foot With Prototypes!" ...

  3. 216. 组合总和 III

    216. 组合总和 III 题意 找出所有相加之和为 n 的 k 个数的组合.组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字. 说明: 所有数字都是正整数. 解集不能包含重复的 ...

  4. SGU 403 Scientific Problem

    403. Scientific Problem Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes input: stan ...

  5. tyvj:1038 忠诚 线段树

    tyvj:1038 忠诚 Time Limit: 1 Sec  Memory Limit: 131072KiBSubmit: 9619  Solved: 3287 题目连接 http://www.ty ...

  6. 活动(Activity)

    一.用Log打印日志 Log.d("HelloWorldActivity", "onCreate execute"); 二.Toast用法 Toast.make ...

  7. HDU 4709 Herding (枚举)

    Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. Complete list of APDU responses

    https://www.eftlab.com.au/index.php/site-map/knowledge-base/118-apdu-response-list List of APDU resp ...

  9. 【windows socket+HTTPserverclient】

    Windows Socket+HTTPserverclient      Winsock是 Windows下套接字标准.                 1.HTTP协议:          HTTP ...

  10. C# 转换图形为PCX 格式

    2010-5-27 PCX RLE压缩图形的行对齐比.NET多了一位.已经修正了. 2009 -7-25 C# 转换图形为PCX 格式 增加了对1位色的PCX的读取 2009-6 -12 RLE数据压 ...