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. 设置启动MyEclipse8.5的时候选择工作区间

    以前我的MyEclipse启动的时候默认都会进入到指定工作区间,比如:D:\MyEclipse,最近不知道怎么回事,每次启动的时候都是空的,必须要重新Switch Workspace选择一次 网上查找 ...

  2. node.js系列笔记之fs模块《二》

    一:感触 最近工作比较忙,感觉也比较多,因为工作上的不顺利,再加上加班比较多,所以最近心情不是很好,再加上英语能力差到不行,所以最近半个月学习进度也比较慢, 但还是告诉自己每天都坚持学一点,即使今天心 ...

  3. LLVM小结

    随笔- 5  文章- 0  评论- 10  LLVM小结   如果说gcc是FSF的传奇,llvm就是Chris Lattner的小清新.当然啦,想具体看看这位四处游山玩水还GPA 4.0的大神和他的 ...

  4. [转]SQL Server中临时表与表变量的区别

    [转]http://blog.csdn.net/skyremember/archive/2009/03/05/3960687.aspx 我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是 ...

  5. jQuery Mobile (整合版)

    jQuery Mobile (整合版) 前言 为了方便大家看的方便,我这里将这几天的东西整合一下发出. 里面的例子请使用手机浏览器查看. 什么是jQuery Mobile? jquery mobile ...

  6. Vim 7.4.1952 with Python/Ruby/Lua/Perl/C Syntax built for Ubuntu 16.04 x86_64

    The default Vim provided by Ubuntu 16.04 even did not have Python support. That's insane. I say, wha ...

  7. python schedule processor

    run some tasks which could look like CRON within linux/UNIX in python. Here's a demo which run on ub ...

  8. 结构-行为-样式-Jquery实现延迟加载特效(数据缓冲特效)

    最近在做一个地产项目的过程中,原来用的延迟加载的插件在IE下会使浏览器突然缩小,这个让客户很不满意,于是就考虑到兼容性的问题决定自己写一个插件.思路:定义一个代码块,手动加载到页面,然后手动删除.   ...

  9. CodeForces 747E Comments

    栈,模拟. 手动写一个栈模拟一下过程即可. #include<cstdio> #include<cstring> #include<string> #include ...

  10. 安卓自动化测试工具一:Monkey

    一:monkey的用途:主要用于稳定性测试,模拟用户操作 二.monkey的基本使用 monkey文档地址:"<android_sdk>/docs/tools/help/monk ...