1、首先得配置一下Struts得配置文件struts-xml:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts>
<!-- 设置可否动态调用方法 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 设置访问路径的后缀名 -->
<constant name="struts.action.extension" value="do,action"/> <!-- 设置上传文件的最大值 10M左右-->
<constant name="struts.multipart.maxSize" value="10701096"/> <!--
上传单个文件
-->
<package name="uploadfile" namespace="/uploadfile" extends="struts-default">
<action name="uploadfile" class="it.web.action.UploadFileAction" method="execute">
<result name="success">/WEB-INF/demo/uploadfile.jsp</result>
</action>
</package> <!--
上传多个文件
-->
<package name="uploadfiles" namespace="/uploadfiles" extends="struts-default">
<action name="uploadfiles" class="it.web.action.UploadFileAction" method="execute">
<result name="success">/WEB-INF/demo/uploadfiles.jsp</result>
</action>
</package> </struts>

2、对应的action类为:

 package it.web.action;

 import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; public class UploadFileAction {
//上传单个文件字段
private File image; //文件(必须要和表单的name属性值一样)
private String imageFileName; //文件名称(表单的name属性值+FileName)
private String imageContentType;//得到上传文件的类型(表单的name属性值+ContentType) //上传多个文件的字段
private File[] images;
private String[] imageFileNames;
private String[] imagesContentType;
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 File[] getImages() {
return images;
}
public void setImages(File[] images) {
this.images = images;
} public String[] getImageFileNames() {
return imageFileNames;
}
public void setImageFileNames(String[] imageFileNames) {
this.imageFileNames = imageFileNames;
} public String[] getImagesContentType() {
return imagesContentType;
}
public void setImagesContentType(String[] imagesContentType) {
this.imagesContentType = imagesContentType;
} /*
* 上传单个文件
*/
public String execute() throws Exception{
//image
/*
* 得到上传文件的真实路径
*/
String realpath = ServletActionContext.getServletContext().getRealPath("/image");
System.out.println(realpath); //判断文件是否存在
if(image!=null){
/*
* 创建文件虚拟的
*
* new File(realpath):文件路径
*
* imageFileName:文件名称
*
*/
File savefile = new File(new File(realpath), imageFileName); if(!savefile.getParentFile().exists())
{
savefile.getParentFile().mkdir(); FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "文件上传成功");
}
}
return "success";
} /*
* 上传多个文件
*/
public String execute1() throws Exception{
//image
/*
* 得到上传文件的真实路径(需要保存的路径)
*/
String realpath = ServletActionContext.getServletContext().getRealPath("/image");
System.out.println(realpath); //判断文件是否存在
if(images!=null){
/*
* 创建文件虚拟的
*
* new File(realpath):文件路径
*
* imageFileName:文件名称
*
*/
File savedir = new File(realpath); if(!savedir.getParentFile().exists())
{
savedir.mkdirs(); for(int i=0;i<images.length;i++)
{
File savefile = new File(savedir,imageFileNames[i]);
FileUtils.copyFile(images[i], savefile);
}
ActionContext.getContext().put("message", "多个文件文件上传成功");
}
}
return "success";
}
}

3、对应的上传单个文件的jsp页面为:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head> <body>
<form action="${pageContext.request.contextPath}/uploadfile/uploadfile" enctype="multipart/form-data" method="post">
文件:<input type="file" name="image"/>
<input type="submit" value="上传"/><br>
文件类型:${imageContentType}
文件上传状态:${message}
</form>
</body>
</html>

4、对应的上传多个文件的jsp页面为:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="${pageContext.request.contextPath}/uploadfiles/uploadfiles" enctype="multipart/form-data" method="post">
文件:<input type="file" name="images"/><br>
<input type="file" name="images"/><br>
<input type="file" name="images"/>
<input type="submit" value="上传"/><br>
文件上传状态:${message}
</form>
</body>
</html>

注:项目中用到的一些jar包如下:

Struts2中实现文件上传的功能的更多相关文章

  1. struts2中的文件上传,文件下载

    文件上传: Servlet中的文件上传回顾 前台页面 1.提交方式post 2.表单类型 multipart/form-data 3.input type=file 表单输入项 后台 apache提交 ...

  2. 4.struts2中的文件上传,下载

    Struts2中文件的上传下载,是借用commons里面的包实现文件的上传,需要导入两个jar commons-fileupload-1.2.2.jar commons-io-2.0.1.jar 实现 ...

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

    天下大事,必做于细.天下难事,必作于易. 以前见过某些人,基础的知识还不扎实就去学习更难的事,这样必定在学习新的知识会非常迷惑结果 再回来又一次学习一下没有搞懂的知识,这必定会导致学习效率的下降!我写 ...

  4. struts2中的文件上传和文件下载

    单文件文件上传 1.

  5. javaweb项目中的文件上传下载功能的实现

    框架是基于spring+myBatis的. 前台页面的部分代码: <form action="${ctx}/file/upLoadFile.do"method="p ...

  6. 笨鸟先飞之Java(一)--使用struts2框架实现文件上传

    无论是.net还是Java,我们最常接触到的就是文件的上传和下载功能,在Java里要实现这两个经常使用功能会有非常多种解决方案,可是struts2的框架却能给我们一个比較简单的方式,以下就一起来看吧: ...

  7. JavaWeb中的文件上传和下载功能的实现

    导入相关支持jar包:commons-fileupload.jar,commons-io.jar 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上 ...

  8. Struts2 之 实现文件上传和下载

    Struts2  之 实现文件上传和下载 必须要引入的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 01.文件上传需要分别在struts.xm ...

  9. 转:在Struts 2中实现文件上传

    (本文转自:http://www.blogjava.net/max/archive/2007/03/21/105124.html) 前一阵子有些朋友在电子邮件中问关于Struts 2实现文件上传的问题 ...

随机推荐

  1. 使用entity framework开发oracle

    A.vs2010 SP1 B.ODAC(http://www.oracle.com/technetwork/database/windows/downloads/index-101290.html) ...

  2. 最小堆实现优先队列:Python实现

    最小堆实现优先队列:Python实现 堆是一种数据结构,因为Heapsort而被提出.除了堆排序,“堆”这种数据结构还可以用于优先队列的实现. 堆首先是一个完全二叉树:它除了最底层之外,树的每一层的都 ...

  3. FpGrowth算法

    FpGrowth算法 频繁项集与关联规则挖掘(2)--FpGrowth算法   上一篇介绍了关联规则挖掘的一些基本概念和经典的Apriori算法,Aprori算法利用频繁集的两个特性,过滤了很多无关的 ...

  4. Javascript:由 “鸭子类型” 得出来的推论

    Javascript:由 “鸭子类型” 得出来的推论 背景 学动态语言的都知道一句话:“如果它走起来像鸭子,而且叫起来像鸭子,那么它就是鸭子”,Javascript也支持鸭子类型,下文就说说鸭子类型在 ...

  5. 分布式EventBus的Socket实现 - 发布订阅

    分布式EventBus的Socket实现 - 发布订阅 在这篇文章中,EventBus实现 - 发布订阅 - XML加载 所适用的范围只是本机的事件传播,要是牵涉到多台服务器之间的事件传播就不行了,解 ...

  6. C# 代理应用 - Cachable

    C# 代理应用 - Cachable 放心,这次不是说设计模式中的代理模式,说的是C#的RealProxy的用法,主要用于:通过给class贴标签,让class做更多的工作,比如判断是否存在缓存,有则 ...

  7. Http的四种post方式

    1.引言 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交 ...

  8. PHP, Python Nginx works together!

    Nginx is so good at delivering requests to many others. Good! Now let's use the nginx upstream modul ...

  9. HDU 2040 亲和数

    Problem Description 古希腊数学家毕达哥拉斯在自然数研究中发现,220的所有真约数(即不是自身的约数)之和为: 1+2+4+5+10+11+20+22+44+55+110=284. ...

  10. 移植rtmpdump(librtmp)到android

    编译环境:(rtmpdump-master.zip和Polar SSL版本已经打包上传,具体路径在http://download.csdn.net/detail/gyley2/5721061) win ...