文件上传是web应用中一种常见的功能,有着很广泛的作用,下面我就分享一下我写的一个struts2上传案例,通过案例,我们可以快速掌握struts2的上传-----------------------------------

  先写好上传所需的jsp页面:

      上传页面(index.jsp)-----

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%  String path = request.getContextPath();  String basePath = request.getScheme() + "://"  + request.getServerName() + ":" + request.getServerPort()  + path + "/";
%>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">  <title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
</head>  <body>  <div align="center">  <h3>文件上传</h3>  <form action="${pageContext.request.contextPath}/csdn/UploadAction_upload.action" method="post" enctype="multipart/form-data">  上传文件:<input type="file" name="upload"><br>  上传文件:<input type="file" name="upload"> <br />   <input type="submit" value="上传" />  </form>  </div>
</body>
</html>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"><title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
</head><body><div align="center"><h3>文件上传</h3><form action="${pageContext.request.contextPath}/csdn/UploadAction_upload.action" method="post" enctype="multipart/form-data">上传文件:<input type="file" name="upload"><br>上传文件:<input type="file" name="upload"> <br /> <input type="submit" value="上传" /></form></div>
</body>
</html>

上传成功页面(sc.jsp)-----

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>  <head>  <base href="<%=basePath%>">  <title>上传成功页面</title>  <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->  </head>  <body>  <div align="center">  <h3>文件上传成功!!!!</h3>   </div>  </body>
</html>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>上传成功页面</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><div align="center"><h3>文件上传成功!!!!</h3> </div></body>
</html>

上传失败页面(fail.jsp)------

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>  <head>  <base href="<%=basePath%>">  <title>上传失败页面</title>  <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->  </head>  <body>  <div align="center">  <h3>文件上传失败!!!!</h3>   </div>  </body>
</html>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags"  prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>上传失败页面</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><div align="center"><h3>文件上传失败!!!!</h3> </div></body>
</html>

web.xml配置文件--------

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"   xmlns="http://java.sun.com/xml/ns/javaee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <!-- 添加启动struts2MVC框架的过滤器 -->  <filter>  <filter-name>struts2</filter-name>  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>  <filter-mapping>  <filter-name>struts2</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>  <welcome-file-list>  <welcome-file>index.jsp</welcome-file>  </welcome-file-list>
</web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><!-- 添加启动struts2MVC框架的过滤器 --><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

struts.xml文件

<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>  <!-- 常量的配置 -->  <include file="struts-constant.xml" />  <package name="upload" namespace="/csdn" extends="struts-default">  <action name="UploadAction_*" class="www.csdn.struts_upload.action.UploadAction"  method="{1}">  <param name="path">WEB-INF/uploads</param>  <result name="upload">/sc.jsp</result>  <result name="fail">/fail.jsp</result>  </action>  </package>  </struts>  
<?xml version="1.0" encoding="UTF-8">
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts><!-- 常量的配置 --><include file="struts-constant.xml" /><package name="upload" namespace="/csdn" extends="struts-default"><action name="UploadAction_*" class="www.csdn.struts_upload.action.UploadAction"method="{1}"><param name="path">WEB-INF/uploads</param><result name="upload">/sc.jsp</result><result name="fail">/fail.jsp</result></action></package></struts>

struts-constant.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>  <!-- 常量的配置 -->  <!-- struts2的后缀 -->  <constant name="struts.action.extension" value="action"/>  <!-- 编码方式 -->  <constant name="struts.i18n.encoding" value="UTF-8"/>  <!-- 浏览器静态缓存最好处于关闭状态 -->  <constant name="struts.serve.static.browserCache" value="false"/>  <!-- struts.xml文件当被修改后 重新加载,开发阶段最好打开 -->  <constant name="struts.configuration.xml.reload" value="true"/>    <!-- 处于开发阶段 最好把开发模式打开 会打印更多的详细错误信息 -->  <constant name="struts.devMode" value="true"/><SPAN style="COLOR: #6600cc"><STRONG><SPAN style="FONT-SIZE: 14px">
</SPAN></STRONG></SPAN></struts>  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd">
<struts><!-- 常量的配置 --><!-- struts2的后缀 --><constant name="struts.action.extension" value="action"/><!-- 编码方式 --><constant name="struts.i18n.encoding" value="UTF-8"/><!-- 浏览器静态缓存最好处于关闭状态 --><constant name="struts.serve.static.browserCache" value="false"/><!-- struts.xml文件当被修改后 重新加载,开发阶段最好打开 --><constant name="struts.configuration.xml.reload" value="true"/>  <!-- 处于开发阶段 最好把开发模式打开 会打印更多的详细错误信息 --><constant name="struts.devMode" value="true"/>
</struts>

UploadAction文件

package www.csdn.struts_upload.action;  import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;  import com.opensymphony.xwork2.ActionSupport;  public class UploadAction extends ActionSupport {  /**  *   */  private static final long serialVersionUID = 1L;  private File[] upload; // 上传的文件  private String[] uploadContentType;// 文件类型  private String[] uploadFileName;// 文件的名称  private String path;  public String getPath() {  return ServletActionContext.getServletContext().getRealPath(path);  }  public void setPath(String path) {  this.path = path;  }  public File[] getUpload() {  return upload;  }  public void setUpload(File[] upload) {  this.upload = upload;  }  public String[] getUploadContentType() {  return uploadContentType;  }  public void setUploadContentType(String[] uploadContentType) {  this.uploadContentType = uploadContentType;  }  public String[] getUploadFileName() {  return uploadFileName;  }  public void setUploadFileName(String[] uploadFileName) {  this.uploadFileName = uploadFileName;  }  public String upload() {  File file = new File(getPath());  if (!file.exists()) {  file.mkdirs();  }  try {  if (upload != null) {  for (int i = 0; i < upload.length; i++) {  File uploadFile = upload[i];  FileUtils.copyFile(uploadFile, new File(file, System.currentTimeMillis()+ "_" + uploadFileName[i]));  }  return "upload";  }  } catch (IOException e) {  e.printStackTrace();  }  return "fail";  }  }  

2011级-csdn-java-张侃— struts2-上传功能相关推荐

  1. 一个小的java作业,第一次上传CSDN,原创的

    自己写的一个小的java作业,第一次上传CSDN,原创的,最近在玩帝国3,然后根据石头剪刀布,炮车骑兵长枪兵的克制规则写了一下,花了一个上午的时间. import java.util.Random;i ...

  2. 给未来的你-李开复在2011级大学新生学习规划讲座上的演讲

    给未来的你 --李开复在2011级大学新生学习规划讲座上的演讲 李开复 很高兴与这么多刚刚入学的大学新生交流,今天的演讲,希望和大家讨论一下,经过大学四年的学习生活,未来的你可能成为什么样子. 有人也 ...

  3. Struts2 - 上传任意多个文件

    Struts2 - 上传任意多个文件 一.上传单个文件 在Struts1.x中已经提供了用于上传文件的组件.而在Struts2中提供了一个更为 容易操作的上传文件组件.所不同的是,Struts1.x的 ...

  4. java获取ajax上传的文件,Java使用Ajax异步上传文件

    相关代码示例: html代码片段: 名称 class="layui-input"> 描述 文件 请选择配置文件 立即提交 重置 js代码片段: //上传配置文件 $(&quo ...

  5. java语言 文件上传,java中实现文件上传的方法

    java中实现文件上传的方法 发布时间:2020-06-19 10:29:11 来源:亿速云 阅读:86 作者:Leah 这篇文章给大家分享的是java中实现文件上传的方法,相信大部分人都还没学会这个 ...

  6. flex java red5_使用 Flex 和Java servlets 将文件上传到 RED5 服务器的步骤

    本文使用一个 demo 来演示如何使用 Flex 和 Java servlets 上传一个任意类型的本地文件到 RED5 服务器.使用的是 Flex 的 FileReference 接口,该接口可以对 ...

  7. java里面的文件上传与下载

    java里面的文件上传与下载 文件的上传与下载主要用到两种方法: 1.方法一:commons-fileupload.jar commons-io.jar apache的commons-fileuplo ...

  8. java实现多文件上传至本地服务器

    转载自  java实现多文件上传至本地服务器 博主最近在做一个内网项目,内部可以访问外部数据,但是外部访问不了内部数据,这也就造成了可能文件无法上传,所以博主另辟蹊径,在本地服务器上建立一个文件夹专门 ...

  9. ckeditor java 上传_java使用CKEditor实现图片上传功能

    java如何使用ckeditor实现图片上传功能,具体内容如下 1.根据实际需要下载指定的ckeditor 2.删除文件ckeditor/plugins/image/dialogs/image.js预 ...

  10. java读excel乱码,【java 项目中,上传的excel打开时无法正常打开,显示乱码 ,怎样可以正常打开,】java读取excel乱码...

    java 项目中,上传的excel打开时无法正常打开,显示乱码 ,怎样可以正常打开, 1.转换格就是将受损的Excel XP簿另存格式选为SYLK.如果可以打开受件,只是不能进行各种编辑和打印操作,那 ...

最新文章

  1. 安卓开发首次创建项目一直转圈_Android视频开发进阶(part3Android的Media API)
  2. 为什么手机联系人里有permission_为什么有人在咖啡馆里不看手机不看电脑没有同伴,独自喝咖啡?...
  3. JavaEE实战班第二天
  4. c语言1A,C程序设计语言1a.ppt
  5. Object C学习笔记15-协议(protocol)
  6. 【报告分享】清华大学126页PPT:2020-2021年元宇宙发展研究报告.pdf(附下载链接)...
  7. cocos2dx 物理碰撞
  8. jQuery实现彩色云标签
  9. [论文评析]ArXiv,2021, CrossFormer技术分析
  10. win7打不开桌面计算机,win7电脑中梦幻桌面打不开如何解决
  11. 全球及中国卫星产业应用建设布局及投资机会分析报告2022-2028年版
  12. 谷歌大脑2017技术研究总结 | Jeff Dean执笔(附论文 数据集)
  13. java stringbuilder_Java stringBuilder的使用方法及实例解析
  14. Spring Security 4 使用@PreAuthorize,@PostAuthorize, @Secured, EL实现方法安全(带源码)
  15. Redis资料汇总(二) 数据类型
  16. 计算机毕业设计Node.js+Vue交通违章举报平台(程序+源码+LW+部署)
  17. 教育培训行业市场营销推广的主要方式有哪些
  18. 7-1 计算存款利息 (10分) 本题目要求计算存款利息
  19. 为什么Java大数据是最火爆的编程语言?
  20. 区块链溯源系统开发:为何百度、阿里纷纷押注区块链溯源

热门文章

  1. 单片机酒精浓度测试仪硬件软件,基于单片机的酒精浓度检测仪的设计(附实物图,原理图接线图,仿真程序)...
  2. Mac上必备的思维导图:MindNode Mac中文版
  3. 鸿蒙系统支持和汽车互联吗,华为公布三大鸿蒙车载操作系统
  4. Unsupervised Scalable Representation Learning for Multivariate Time Series
  5. mscorwks.dll没有被指定在Windows上运行,或者它包含错误...
  6. 2017最新最有价值的Android视频教程(包括传智播客/最新黑马等视频教程)
  7. HDU1465 不容易系列之一
  8. 如何计算某一地区人民新冠肺炎(COVID-19)的患病概率
  9. 2022年你应该知道的15 款 Python 编辑器/ IDE
  10. CSS+JS实现图片集展示(二)