struts2-上传功能
来源:互联网 发布:第五代软件 编辑:程序博客网 时间:2024/05/21 20:21
文件上传是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>
上传成功页面(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>
上传失败页面(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>
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>
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>
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"/></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";}}
- struts2-上传功能
- struts2文件上传功能
- struts2+xheditor+上传图片功能
- Struts2文件上传功能开发
- struts2实现文件批量上传功能
- struts2实现文件上传功能
- struts2实现图片上传功能
- 【Struts2框架】第八节上传-利用struts2实现上传功能
- struts2文件的上传和下载功能
- 10-Struts2文件上传功能开发
- 关于Struts2的文件上传功能
- 基于struts2实现文件上传功能
- Struts2完成文件的上传功能实例
- Struts2+FileFlashUpload实现多文件上传功能
- struts2文件上传功能的实现
- struts2中实现文件上传功能
- Struts2中实现多文件上传功能
- 在struts2框架做上传功能
- 关于strncpy
- Android多媒体浅析
- VS2010如何修改项目名称
- POJ1789《Truck History》方法:Prim
- 父亲网上卖掉2岁儿子 奶奶报警后追回
- struts2-上传功能
- 今天在清华图书馆看到我的杰作,感慨万千,而我要归零一切 !
- 冒泡排序的C语言实现
- onMeasure方法理解
- Resource file 和Source file 区别
- 使用Swing编写win7简易计算器4——双目运算符
- Android多媒体支撑库OpenCore视频硬件加速(转)
- 红帽集群套件应用实例
- 处理"网页已经过期"问题