2011级-csdn-java-张侃— struts2-上传功能
来源:互联网 发布:ubuntu u盘 卡在启动 编辑:程序博客网 时间:2024/05/22 17:02
文件上传是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-上传功能
- 2011级-csdn-java-张侃—struts2文件上传错误信息国际化的处理(案例)
- 2011级-csdn-java-张侃—Struts2了解
- 2011级-csdn-java-张侃— struts2的优点
- 2011级-csdn-java-张侃— Struts2.0国际化
- 2011级-csdn-java-张侃— Struts2——HelloWorld
- 2011级-csdn-java-张侃— JDBC开发—文件的上传和下载(一)
- 2011级-csdn-java-张侃— JDBC开发—文件的上传和下载(二)
- 2011级-csdn-java-张侃—第十一周周报
- 2011级-csdn-java-张侃— 过滤器
- 2011级-csdn-java-张侃— Servlet事件监听器
- 2011级-csdn-java-张侃—Spring(1)
- 2011级-csdn-java-张侃—Spring(2)
- 2011级-csdn-java-张侃— JDBC开发—连接池(一)
- 2011级-csdn-java-张侃— JDBC开发—连接池(二)
- 2011级-csdn-java-张侃—自定义JSP标签(一)
- 2011级-csdn-java-张侃—自定义JSP标签(二)
- 2011级-csdn-java-张侃—JSP标准标签库
- 使用脚本运行jar包时不显示cmd窗口
- Objective-C 数组对象(七)
- Apache安装出现的问题的解决
- 教你去视频网站的开始广告
- Java中Xml解析详解 DOM、SAX、JDOM、DOM4J
- 2011级-csdn-java-张侃— struts2-上传功能
- Android_View,ViewGroup,Window之间的关系
- Linux C编程--进程介绍5--system函数
- 从Xcode编译时自带的“图片压缩”说起
- 进程及其进程环境
- The Bronte Story——3、The little books
- Java二维数组的三种表示形式
- git 代码协同之回车问题 CRLF LF
- Objective-C 词典对象(八)