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. Redis系统学习 二、数据结构

    一.字符串     1.在Redis里,字符串是最基本的数据结构.当你在思索着关键字-值对时,你就是在死锁着字符串数据结构.不要被名字给搞混了. 常见实例: set users:leto " ...

  2. 简单使用JSON,JavaScript读取JSON文本(三)

    JavaScript 读取 JSON 文本转换为对象 JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 JSON 数据转换为 Jav ...

  3. HTML5 Canvas中实现绘制一个像素宽的细线

    正统的HTML5 Canvas中如下代码 ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(10, 100); ctx.lineTo(300,100); c ...

  4. Jquery EasyUI tabs处理

    一 获取选中的 Tab 1.   // 获取选中的 tab panel 和它的 tab 对象 2.  var pp = $('#tt').tabs('getSelected'); 3.  var ta ...

  5. Trie树及其应用

    Trie树及其应用 Trie树 Trie树,又称单词查找树.字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经 ...

  6. [每日一题] OCP1z0-047 :2013-07-19 Rules of Precedence――括号的使用

    这道题目的意思是你的公司决定给所有呆到5年或5年以上的所有员工每个月加50美元,然后算出总的年薪.每个月薪水:salary,每个月加到:salary+50,总的年薪: (salary+50)*12. ...

  7. Hadoop Streaming Command Details and Q&A

    Hadoop Streaming Hadoopstreaming is a utility that comes with the Hadoop distribution. The utilityal ...

  8. Python基础1-变量、运算符、表达式

    一.Python的安装 1.下载python安装包https://www.python.org/ 2.选择对应的Python版本(Windows下) 3.装完之后打开电脑的cmd,验证一下安装是否成功 ...

  9. HTML5 智能表单

    HTML5 智能表单 1.表单新增属性  ☀ autofocus 属性 <input type="text" autofocus/>设置 autofocus 属性,使文 ...

  10. HDU--1006

    题目介绍 Problem Description The three hands of the clock are rotating every second and meeting each oth ...