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. 两个本地(localhost)html文件之间的传值

    什么是iframe? iframe 元素会创建包含另外一个文档的内联框架(即行内框架).可以理解为把iframe解释成“浏览器中的浏览器“ 在IE中: document.frames[i].docum ...

  2. MapKit的使用显示当前位置

    1.添加MapKit.framework框架 ,在plist中添加字段,用于,获取用户当前位置设置 NSLocationAlwaysUsageDescription 2.代码 #import &quo ...

  3. 用Access作为后台数据库支撑。

    /// <summary> /// 读取Excel文档 /// </summary> /// <param name="Path">文件名称&l ...

  4. SQL作业的操作全

    --定义创建作业 转自http://hi.baidu.com/procedure/blog/item/7f959fb10d76f95d092302dd.html DECLARE @jobid uniq ...

  5. HDFS & MapReduce异构存储性能测试白皮书

  6. clone github的代码

    终端执行:git clone 连接.git     #不用sudo

  7. 移动Web开发规范

    1.字体设置 使用无衬线字体 body { font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif; } 2.设 ...

  8. open文件操作

    open()做文件操作的就是他1.打开文件#f=open("db","r")#只读#f-open("db","w")#只 ...

  9. css -- 映像 ,分页(上一页下一页)

    1.映像:-webkit-box-reflect:blow 2px -webkit-gradient( linear, left top, left bottom, from(transparent) ...

  10. NumPy 学习(2): 数组的操作

    1. 简单一维数组的操作 一维数组的操作类似于python自身的list类型. In [14]: arr = np.arange(10) In [15]: arr Out[15]: array([0, ...