(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. windows下thrift的使用(python)

    1.下载thrift,下载地址:http://archive.apache.org/dist/thrift/0.9.3/ 2.在编写python的thrift代码时,需要先安装thrift modul ...

  2. 【CF540D】 D. Bad Luck Island (概率DP)

    D. Bad Luck Island time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

    题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...

  4. 20162327WJH实验四——图的实现与应用

    20162327WJH实验四--图的实现与应用 实 验 报 告 课程:程序设计与数据结构 班级: 1623 姓名: 王旌含 学号:20162327 成绩: 指导教师:娄嘉鹏 王志强 实验日期:11月2 ...

  5. 删除JBOSS eap4.3下的jmx-console、web-console、ws-console、status服务

    来源:http://iffiffj.iteye.com/blog/1404148 把下面代码保存为BAT文件,并放到JBOSS节点中运行. @echo off set HOME=%~dp0 set j ...

  6. 对于asp.net mvc异步查询

    如何做MVC异步查询,做列表页面. 查询是项目中必不可少的工作,而且不同的项目不同的团队,都有自己的简单方法.Asp.net mvc 有自己独特的优势,下面是结合mvc实现一个产品列表的Demo. 问 ...

  7. 华为S5300系列交换机V100R006SPH017VRP热补丁

    S5300_V100R006SPH017.pat 附件: 链接:https://pan.baidu.com/s/16lrNMykatXR3_5xKBc2zuw  密码:rt1l

  8. How to exit the entire application from a Python thread?

    If all your threads except the main ones are daemons, the best approach is generally thread.interrup ...

  9. mongodb exception in initAndListen: 12596 old lock file, terminating解决方法

    错误信息如下: exception old lock file, terminating 解决方法 .删除data目录中的.lock文件 .mongod.exe --repair .启动mongod就 ...

  10. FolderSync文件夹同步

    FolderSync是一款支持各大国外网盘同步的软件,目前支持 SkyDrive, Dropbox, SugarSync, Ubuntu One, Box.net, LiveDrive, HiDriv ...