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. VirtualBox安装CentOS6.4(32bit)

    实验环境 Win7 64bit 目的: 实验VirtualBox安装CentOS6.4(32bit) 下载VirtualBox 地址: http://www.oracle.com/technetwor ...

  2. SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析)

    SQL查询表的行列转换/小计/统计(with rollup,with cube,pivot解析) 2013-8-20 1.    SQL查询表的行列转换/小计/统计(with  rollup,with ...

  3. 持续集成环境Gitlab-CI的官方安装过程解析

    持续集成环境是一个非常重要的工具,在分工合作的项目中有着举足轻重的作用.公司最近要用Gitlab,需要配套的持续集成环境.研究了官方的文档,感觉官方的文档不是很明了.各种修改过后终于成功了.为了大家安 ...

  4. struts2对ognl表达式的使用(配图解加讲解)

    ognl它是一个功能强大的表达式语言,用来获取和设置Java对象的属性,它旨在提供一个更高的更抽象的层次来对Java对象图进行导航. 先看一张示意图 如果是下面的除了第一种valueStack的下面几 ...

  5. Java Concurrency (1)

    Memory that can be shared betweenthreads is called shared memory or heap memory. The term variable a ...

  6. jquery代码实现简单的五星评价功能!

    实现: 1,鼠标移动到第三个星星,则一二三星星变亮,后两个变暗 2,鼠标点击某个星星后,可以继续选择,但拿开后星星会定格住你点击的位置 <script type="text/javas ...

  7. Hadoop学习之Ubuntu12.04 Hadoop 环境搭建笔记

    SSH无密码配置 Hadoop在Ubuntu12.04上搭建环境 报错及问题 SSH无密码配置 参考:Linux(Centos)配置OpenSSH无密码登陆 注意问题: Hadoop集成环境三台机器都 ...

  8. 用vue实现简单实时汇率计算功能

    最近在自己摸索vue的使用,因为相对于只是去看教程和实例,感觉不如自己动手写一个demo入门来的快.刚好看到小程序中有一个简单但是很精致的应用极简汇率,而且它的表现形式和vue的表现形式很像,于是想着 ...

  9. C语言之原码、反码和补码

    原码.反码和补码 1).数据在内存中存储的时候都是以二进制的形式存储的. int num = 10; 原码.反码.补码都是二进制.只不过是二进制的不同的表现形式. 数据是以补码的二进制存储的. 2). ...

  10. [ios-必看] 国人当自强:两岸三地在线编程学习网站大搜罗 [转]

    http://blog.csdn.net/lyy_whg/article/details/17350923 说到国内的在线编程学习网站,很多人都是一脸茫然,即使是资深开发者也是如此.在许多人眼中,尽管 ...