(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. BZOJ 4289: PA2012 Tax 差分建图 最短路

    https://www.lydsy.com/JudgeOnline/problem.php?id=4289 https://www.cnblogs.com/clrs97/p/5046933.html  ...

  2. 仅100行的JavaScript DOM操作类库

    如果你构建过Web引用程序,你可能处理过很多DOM操作.访问和操作DOM元素几乎是每一个Web应用程序的通用需求.我们我们经常从不同的控件收集信息,我们需要设置value值,修改div或span标签的 ...

  3. Linux下动态库和静态库的生成和使用

    1.准备头文件和源文件 hello.h #ifndef HELLO_H #define HELLO_H void hello(const char *name): #endif hello.c #in ...

  4. 【转载】HTTP/FTP客户端开发库:libwww、libcurl、libfetch

    网页抓取和ftp访问是目前很常见的一个应用需要,无论是搜索引擎的爬虫,分析程序,资源获取程序,WebService等等都是需 要的,自己开发抓取库当然是最好了,不过开发需要时间和周期,使用现有的Ope ...

  5. HTML的各个标签的默认样式

    head{ display: none } body{ margin: 8px;line-height: 1.12 } button, textarea,input, object,select { ...

  6. 如何添加mysql到环境变量

    环境: 在自己安装的lampp环境下,当使用mysql的时候必须指定路径才能进入数据库:这样显得太过麻烦.我们可以通过将mysql加入到环境变量中来解决该问题(mysql执行路径/opt/lampp/ ...

  7. Caffe2(2)----Eclipse环境中使用Caffe2

    使用IDE开发深度学习的应用,可以事半功倍,Caffe2已经全面支持Python,这里介绍如何在Ubantu14.04下,利用EclipseCaffe2的二次开发或应用. 1.安装eclipse 具体 ...

  8. Oracle问题诊断过程常用SQL

    --查看临时表空间使用情况select tablespace_name ,sum(size_mb),sum(used_mb),round(sum(used_mb)/sum(size_mb),2) fr ...

  9. IIS与Asp.net

    一.IIS 1.绑定 为了将特定的请求映射到相应的网站,IIS允许我们配置“绑定”.所谓“绑定”就是将一个特定的地址.端口号和HTTP主机名对应到特定的网站. IIS7添加绑定的代码如下图所示: 在I ...

  10. 3. python 字符串的一般使用

    3. python 字符串的一般使用 1.基本操作 1)使用+连接 >>> "abc"+"efg"    'abcefg'    >&g ...