1、HTML代码

<body>

<!--上传一个文件   enctype="multipart/form-data" 上传文件必须设置这个属性和属性值-->
<form action="singleUpload!upload" method="post" enctype="multipart/form-data">
文件:<s:file name="img"></s:file><br>
<input type="submit" value="上传" />
</form>
<hr>

//对应action代码

public class SingleUploadAction extends ActionSupport implements ServletContextAware {

private ServletContext app;
private File img;//收集上传文件
public File getImg() {
return img;
}
public void setImg(File img) {
this.img = img;
}
public String getImgFileName() {
return imgFileName;
}
public void setImgFileName(String imgFileName) {
this.imgFileName = imgFileName;
}
public String getImgContentType() {
return imgContentType;
}
public void setImgContentType(String imgContentType) {
this.imgContentType = imgContentType;
}
private String imgFileName;//固定命名方式,xxxFileName来得到上传的文件名
private String imgContentType;//固定命名方式,xxxContentType得到文件类型;

public String upload(){
String path = app.getRealPath("image");//这里我们在WebRoot中建一个image文件夹
File to = new File(path+"\\"+imgFileName);//文件保存的目标位置
try {
//将用户上传的文件保存到目标位置
FileUtils.copyFile(img, to);
} catch (IOException e) {
e.printStackTrace();
}
return this.SUCCESS;
}
public void setServletContext(ServletContext context) {
this.app = context;
}
}

<!--同时上传多个文件-->
<form action="multiUpload!upload" method="post" enctype="multipart/form-data">
文件:<s:file name="img"></s:file><br>
文件:<s:file name="img"></s:file><br>
文件:<s:file name="img"></s:file><br>
<input type="submit" value="上传" />
</form>
</body>

//对应action 代码

public class MultiUploadAction extends ActionSupport implements ServletContextAware {
private ServletContext app;
private File[] img;
private String[] imgFileName;//固定命名方式,xxxFileName来得到上传的文件名数组
private String[] imgContentType;//固定命名方式,xxxContentType得到文件类型数组;
public File[] getImg() {
return img;
}
public void setImg(File[] img) {
this.img = img;
}
public String[] getImgFileName() {
return imgFileName;
}
public void setImgFileName(String[] imgFileName) {
this.imgFileName = imgFileName;
}
public String[] getImgContentType() {
return imgContentType;
}
public void setImgContentType(String[] imgContentType) {
this.imgContentType = imgContentType;
}
public void setServletContext(ServletContext context) {
this.app = context;
}

public String upload(){
for (int i = 0; i < img.length; i++) {
String path = app.getRealPath("image");
File to = new File(path+"\\"+imgFileName[i]);//文件保存的目标位置
try {
//将用户上传的文件保存到目标位置
FileUtils.copyFile(img[i], to);
} catch (IOException e) {
e.printStackTrace();
}
}
return this.SUCCESS;
}
}

Struts2中上传图片案列的更多相关文章

  1. 关于ueditor 在struts2 中 上传图片 ,未找到上传文件 问题的解决方法

    问题原因: ueditor 上传图片需请求imageUp.jsp文件,struts2 自带的拦截器(/*)把所有请求的文件都做了处理,所以导致无法上传图片. 解决方法: 方法一:自定义拦截器,让它在请 ...

  2. ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案

    摘要: ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案 在struts2应用中使用ueditor富文本编辑器上传图片或者附件时,即使配置 ...

  3. Tensorflow 中(批量)读取数据的案列分析及TFRecord文件的打包与读取

    内容概要: 单一数据读取方式: 第一种:slice_input_producer() # 返回值可以直接通过 Session.run([images, labels])查看,且第一个参数必须放在列表中 ...

  4. Spring MVC的配置文件(XML)的几个经典案列

    1.既然是配置文件版的,那配置文件自然是必不可少,且应该会很复杂,那我们就以一个一个的来慢慢分析这些个经典案列吧! 01.实现Controller /* * 控制器 */ public class M ...

  5. Struts2中的EasyUI

    Struts2中的EasyUI 一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tre ...

  6. js闭包的作用域以及闭包案列的介绍:

    转载▼ 标签: it   js闭包的作用域以及闭包案列的介绍:   首先我们根据前面的介绍来分析js闭包有什么作用,他会给我们编程带来什么好处? 闭包是为了更方便我们在处理js函数的时候会遇到以下的几 ...

  7. 简单理解Struts2中拦截器与过滤器的区别及执行顺序

    简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...

  8. Struts2中基于Annotation的细粒度权限控制

    Struts2中基于Annotation的细粒度权限控制 2009-10-19 14:25:53|  分类: Struts2 |  标签: |字号大中小 订阅     权限控制是保护系统安全运行很重要 ...

  9. struts2中constant参数设置

    序号 方法 说明 1 <constant name="struts.i18n.encoding" value="UTF-8"/> 指定web应用默认 ...

随机推荐

  1. loj 1379(最短路变形)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27087 思路:题目的意思是求S->T的所有路径中花费总和小于 ...

  2. php代码效率测试

    对于一个被加载的页面,而遇到会卡的原因 ,代码量大,为了减少一句话分析,就采用分段式判断. 从php手册了解到,使用microtime函数,具体方法可参见php手册对这函数的用法 定义get_exec ...

  3. python特殊函数 __cmp__

    __cmp__ 对 int.str 等内置数据类型排序时,Python的 sorted() 按照默认的比较函数 cmp 排序,但是,如果对一组 Student 类的实例排序时,就必须提供我们自己的特殊 ...

  4. 浩瀚技术助力批发零售商户实现PDA移动POS打印扫描进销存信息化管理

    批发零售商户其各门店销售品种多,销售量大,在市场上占据巨大的份额,随着各门店的不断扩展,基层的销售管理并不尽如意,传统的进销存管理软件安装在PC端,无法满足有现有的业务支撑,面对当前现状,移动进销存管 ...

  5. 【转】】Android ADB命令大全

    ADB很强大,记住一些ADB命令有助于提高工作效率. 获取序列号: adb get-serialno 查看连接计算机的设备: adb devices 重启机器: adb reboot 重启到bootl ...

  6. Parameter of Revit API – 19: Add FamilyParameter (FamilyManager.AddParameter)

      New family parameters (FamilyParameter) can be added to a family document through the FamilyManage ...

  7. 【原】iOS学习42即时通信之XMPP(1)

    1. 即时通信 1> 概述 即时通讯(Instant Messaging)是目前Internet上最为流行的通讯方式,各种各样的即时通讯软件也层出不穷,服务提供商也提供了越来越丰富的通讯服务功能 ...

  8. linux中用shell获取时间,日期

    linux中用shell获取昨天.明天或多天前的日期:在Linux中对man date -d 参数说的比较模糊,以下举例进一步说明:# -d, --date=STRING display time d ...

  9. 不刷新改变URL: pushState + Ajax

    如果你玩过Google+,看到过YouTube的新界面,便会体验到这个HTML5的新功能.使用pushState + Ajax(pjax),可以实现网页的ajax加载,同时又能完成URL的改变而没有网 ...

  10. Mongoose学习参考文档——基础篇

    Mongoose学习参考文档 前言:本学习参考文档仅供参考,如有问题,师请雅正 一.快速通道 1.1 名词解释 Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model ...