注意点

     private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
private String imageFileName;// 上传输入域FileName 文件名
private String imageContentType;// 上传文件的MIME类型

单个文件

 package cn.itcast.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UploadAction1 extends ActionSupport implements Serializable { private File image;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
private String imageFileName;// 上传输入域FileName 文件名
private String imageContentType;// 上传文件的MIME类型 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 getImageContentType() {
return imageContentType;
} public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
} public String execute(){
System.out.println(imageContentType);
try {
//处理实际的上传代码
//找到存储文件的真实路径
// System.out.println(imageFileName);
ServletContext sc = ServletActionContext.getServletContext();
String storePath = sc.getRealPath("/files");
//构建输入输出流
// OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);
// InputStream in = new FileInputStream(image);
// byte b[] = new byte[1024];
// int len = -1;
// while((len=in.read(b))!=-1){
// out.write(b, 0, len);
// }
// out.close();
// in.close(); FileUtils.copyFile(image, new File(storePath,imageFileName)); ActionContext.getContext().put("message", "上传成功!");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}

jsp中

   <body>
<form action="${pageContext.request.contextPath}/upload/upload1.action" method="post" enctype="multipart/form-data">
文件:<input type="file" name="image"/><br/>
<input type="submit" value="上传"/>
</form>
</body>

多个文件上传

 package cn.itcast.action;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UploadAction2 extends ActionSupport implements Serializable { private File[] images;//对应的就是表单中文件上传的那个输入域的名称,Struts2框架会封装成File类型的
private String[] imagesFileName;// 上传输入域FileName 文件名
private String[] imagesContentType;// 上传文件的MIME类型 public File[] getImages() {
return images;
} public void setImages(File[] images) {
this.images = images;
} public String[] getImagesFileName() {
return imagesFileName;
} public void setImagesFileName(String[] imagesFileName) {
this.imagesFileName = imagesFileName;
} public String[] getImagesContentType() {
return imagesContentType;
} public void setImagesContentType(String[] imagesContentType) {
this.imagesContentType = imagesContentType;
} public String execute(){
try { if(images!=null&&images.length>0){
ServletContext sc = ServletActionContext.getServletContext();
String storePath = sc.getRealPath("/files");
for(int i=0;i<images.length;i++)
FileUtils.copyFile(images[i], new File(storePath,imagesFileName[i]));
}
ActionContext.getContext().put("message", "上传成功!");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}

jsp中

   <body>
<form action="${pageContext.request.contextPath}/upload/upload2.action" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="images"/><br/>
文件2:<input type="file" name="images"/><br/>
<input type="submit" value="上传"/>
</form>
</body>

struts.xml中配置

设置文件上传大小

     <constant name="struts.multipart.maxSize" value="52428800"></constant>
     <package name="upload" namespace="/upload" extends="mypackage">
<action name="upload1" class="cn.itcast.action.UploadAction1" method="execute">
<result name="success">/success.jsp</result>
</action>
<action name="upload2" class="cn.itcast.action.UploadAction2" method="execute">
<result name="success">/success.jsp</result>
</action>
</package>
 
 
 

struts2中文件上传的更多相关文章

  1. jsp\struts1.2\struts2 中文件上传(转)

    jsp\struts1.2\struts2 中文件上传 a.在jsp中简单利用Commons-fileupload组件实现 b.在struts1.2中实现c.在sturts2中实现现在把Code与大家 ...

  2. Struts2中文件上传下载实例

    1.单文件上传 jsp页面: <!-- 单文件上传 --> <form action="Fileupload.action" method="post& ...

  3. struts2的文件上传和文件下载

    实现使用Struts2文件上传和文件下载: 注意点: (1)对应表单的file1和私有成员变量的名称必须一致 <input type="file" name="fi ...

  4. 【Struts2】文件上传与下载

    一.上传 1.1 Struts2实现步骤 浏览器端 服务器端 1.2 关于Struts2中文件上传细节: 1.3 示例 jsp文件 Action类 struts.xml文件配置 二.下载 2.1 文件 ...

  5. Struts2之文件上传与下载

    时间:2017-1-11 15:47 --使用commons-fileupload组件上传1.客户端    *   method="post"    *   <input t ...

  6. struts2的文件上传

    在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...

  7. Struts2+Uploadify文件上传使用详解

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例是php版本的,本文将详细介绍Uploadify在java中的使用,您也可以点击下面的链接进行演示或下 ...

  8. Struts2 多文件上传

    Struts2多文件上传只需要将 单文件上传中的File变成File[]  即可,上篇文章:单文件上传 <form action="${pageContext.request.cont ...

  9. Struts2图片文件上传,判断图片格式和图片大小

    1. 配置Struts2能够上传的最大文件大小 使用Struts2进行文件上传的时候,Struts2默认文件大小最大为2MB,如果要传大一点的文件,就需要修改struts.xml配置文件,重新设置能够 ...

随机推荐

  1. grunt-contrib-qunit安装过程中phantomjs安装报错问题解决

    今天自己fork了一个github上别人写的一个关于grunt项目的一个小demo(https://github.com/cowboy/jquery-tiny-pubsub),主要是想学习下grunt ...

  2. Hive 空指针(NPE)异常

    空指针NullPointerException 1 Hive之前的一些BUG [HIVE-9430] - NullPointerException on ALTER TABLE ADD PARTITI ...

  3. [SDOI2005]反素数ant

    题目描述 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4. 如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例如,整数1,2,4,6 ...

  4. Java常用小笔记

    1.对list集合进行分页 //startIndex指的是开始的小标 从0开始,pageSize是每页记录数 int toIndex = new Integer(startIndex)+new Int ...

  5. FFT算法的完整DSP实现

    傅里叶变换或者FFT的理论参考: [1] http://www.dspguide.com/ch12/2.htm The Scientist and Engineer's Guide to Digita ...

  6. 移动端touchstart事件穿透问题,解决方案

    [来源]:在开发移动端网站时,会经常徘徊在click和touchstart之间:因为touchstart虽然好用和快速响应:但是其缺点也是显而易见的,当我们大面积的使用touchstart的时候就会遇 ...

  7. MySQL 连接错误Can't connect to MySQL server on (61)

    链接数据库时忽然遇到一个问题.Mac Navicat链接时报错Can’t connect to MySQL server on ‘xx.xx.xx.xx’ (61). PS. win版Navicat ...

  8. redis节点管理-新增主节点

    原文:http://blog.sina.com.cn/s/blog_53b45c4d0102wg11.html 集群节点添加 节点新增包括新增主节点.从节点两种情况.以下分别做一下测试: 1.新增主节 ...

  9. 通过HttpServletRequest转换获得json对象

    如何把前端传过来的Json对象解析出来?在java web应用中,我们如何获取post请求body中的内容?通常利用request获取参数可以直接通过req.getParameter(name)的方式 ...

  10. jquery dialog close icon missing 关闭图片丢失,样式丢失问题

    http://stackoverflow.com/questions/17367736/jquery-ui-dialog-missing-close-icon