三.在struts2中实现(以图片上传为例)
1.FileUpload.jsp代码清单如下:
Java代码
1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2. <%@ taglib prefix="s" uri="/struts-tags" %>
3. <html>
4. <head>
5. <title>The FileUplaodDemo In Struts2</title>
6. </head>
7.
8. <body>
9. <s:form action="fileUpload.action" method="POST" enctype="multipart/form-data">
10. <s:file name="myFile" label="MyFile" ></s:file>
11. <s:textfield name="caption" label="Caption"></s:textfield>
12. <s:submit label="提交"></s:submit>
13. </s:form>
14. </body>
15. </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>The FileUplaodDemo In Struts2</title>
</head>
<body>
<s:form action="fileUpload.action" method="POST" enctype="multipart/form-data">
<s:file name="myFile" label="MyFile" ></s:file>
<s:textfield name="caption" label="Caption"></s:textfield>
<s:submit label="提交"></s:submit>
</s:form>
</body>
</html>
2.ShowUpload.jsp的功能清单如下:
Java代码
1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2. <%@ taglib prefix="s" uri="/struts-tags" %>
3. <html>
4. <head>
5. <title>ShowUpload</title>
6. </head>
7.
8. <body>
9. <div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
10. <img src ='UploadImages/<s:property value ="imageFileName"/> '/>
11. <br />
12. <s:property value ="caption"/>
13. </div >
14. </body>
15. </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>ShowUpload</title>
</head>
<body>
<div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
<img src ='UploadImages/<s:property value ="imageFileName"/> '/>
<br />
<s:property value ="caption"/>
</div >
</body>
</html>
3.FileUploadAction.java的代码清单如下 :
Java代码
1. package com.chris;
2.
3. import java.io.*;
4. import java.util.Date;
5.
6. import org.apache.struts2.ServletActionContext;
7.
8.
9. import com.opensymphony.xwork2.ActionSupport;
10.
11. public class FileUploadAction extends ActionSupport{
12.
13. private static final long serialVersionUID = 572146812454l ;
14. private static final int BUFFER_SIZE = 16 * 1024 ;
15.
16. //注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定
17. //所以同时要提供myFileContentType,myFileFileName的set方法
18.
19. private File myFile; //上传文件
20. private String contentType;//上传文件类型
21. private String fileName; //上传文件名
22. private String imageFileName;
23. private String caption;//文件说明,与页面属性绑定
24.
25. public void setMyFileContentType(String contentType) {
26. System.out.println("contentType : " + contentType);
27. this .contentType = contentType;
28. }
29.
30. public void setMyFileFileName(String fileName) {
31. System.out.println("FileName : " + fileName);
32. this .fileName = fileName;
33. }
34.
35. public void setMyFile(File myFile) {
36. this .myFile = myFile;
37. }
38.
39. public String getImageFileName() {
40. return imageFileName;
41. }
42.
43. public String getCaption() {
44. return caption;
45. }
46.
47. public void setCaption(String caption) {
48. this .caption = caption;
49. }
50.
51. private static void copy(File src, File dst) {
52. try {
53. InputStream in = null ;
54. OutputStream out = null ;
55. try {
56. in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
57. out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
58. byte [] buffer = new byte [BUFFER_SIZE];
59. while (in.read(buffer) > 0 ) {
60. out.write(buffer);
61. }
62. } finally {
63. if ( null != in) {
64. in.close();
65. }
66. if ( null != out) {
67. out.close();
68. }
69. }
70. } catch (Exception e) {
71. e.printStackTrace();
72. }
73. }
74.
75. private static String getExtention(String fileName) {
76. int pos = fileName.lastIndexOf(".");
77. return fileName.substring(pos);
78. }
79.
80. @Override
81. public String execute() {
82. imageFileName = new Date().getTime() + getExtention(fileName);
83. File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages" ) + "/" + imageFileName);
84. copy(myFile, imageFile);
85. return SUCCESS;
86. }
87. }
package com.chris;
import java.io.*;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 16 * 1024 ;
//注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定
//所以同时要提供myFileContentType,myFileFileName的set方法
private File myFile; //上传文件
private String contentType;//上传文件类型
private String fileName; //上传文件名
private String imageFileName;
private String caption;//文件说明,与页面属性绑定
public void setMyFileContentType(String contentType) {
System.out.println("contentType : " + contentType);
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
System.out.println("FileName : " + fileName);
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() {
imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages" ) + "/" + imageFileName);
copy(myFile, imageFile);
return SUCCESS;
}
}
注:此时仅为方便实现Action所以继承ActionSupport,并Overrider execute()方法
在struts2中任何一个POJO都可以作为Action
4.struts.xml清单如下:
Java代码
1. <?xml version="1.0" encoding="UTF-8" ?>
2. <!DOCTYPE struts PUBLIC
3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4. "http://struts.apache.org/dtds/struts-2.0.dtd">
5. <struts>
6. <package name="example" namespace="/" extends="struts-default">
7. <action name="fileUpload" class="com.chris.FileUploadAction">
8. <interceptor-ref name="fileUploadStack"/>
9. <result>/ShowUpload.jsp</result>
10. </action>
11. </package>
12. </struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="example" namespace="/" extends="struts-default">
<action name="fileUpload" class="com.chris.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<result>/ShowUpload.jsp</result>
</action>
</package>
</struts>
5.web.xml清单如下:
Java代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.4"
3. xmlns="http://java.sun.com/xml/ns/j2ee"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7. <filter >
8. <filter-name > struts-cleanup </filter-name >
9. <filter-class >
10. org.apache.struts2.dispatcher.ActionContextCleanUp
11. </filter-class >
12. </filter >
13. <filter-mapping >
14. <filter-name > struts-cleanup </filter-name >
15. <url-pattern > /* </url-pattern >
16. </filter-mapping >
17.
18. <filter>
19. <filter-name>struts2</filter-name>
20. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
21. </filter>
22. <filter-mapping>
23. <filter-name>struts2</filter-name>
24. <url-pattern>/*</url-pattern>
25. </filter-mapping>
26. <welcome-file-list>
27. <welcome-file>Index.jsp</welcome-file>
28. </welcome-file-list>
29.
30. </web-app>
1.FileUpload.jsp代码清单如下:
Java代码
1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2. <%@ taglib prefix="s" uri="/struts-tags" %>
3. <html>
4. <head>
5. <title>The FileUplaodDemo In Struts2</title>
6. </head>
7.
8. <body>
9. <s:form action="fileUpload.action" method="POST" enctype="multipart/form-data">
10. <s:file name="myFile" label="MyFile" ></s:file>
11. <s:textfield name="caption" label="Caption"></s:textfield>
12. <s:submit label="提交"></s:submit>
13. </s:form>
14. </body>
15. </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>The FileUplaodDemo In Struts2</title>
</head>
<body>
<s:form action="fileUpload.action" method="POST" enctype="multipart/form-data">
<s:file name="myFile" label="MyFile" ></s:file>
<s:textfield name="caption" label="Caption"></s:textfield>
<s:submit label="提交"></s:submit>
</s:form>
</body>
</html>
2.ShowUpload.jsp的功能清单如下:
Java代码
1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2. <%@ taglib prefix="s" uri="/struts-tags" %>
3. <html>
4. <head>
5. <title>ShowUpload</title>
6. </head>
7.
8. <body>
9. <div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
10. <img src ='UploadImages/<s:property value ="imageFileName"/> '/>
11. <br />
12. <s:property value ="caption"/>
13. </div >
14. </body>
15. </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>ShowUpload</title>
</head>
<body>
<div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
<img src ='UploadImages/<s:property value ="imageFileName"/> '/>
<br />
<s:property value ="caption"/>
</div >
</body>
</html>
3.FileUploadAction.java的代码清单如下 :
Java代码
1. package com.chris;
2.
3. import java.io.*;
4. import java.util.Date;
5.
6. import org.apache.struts2.ServletActionContext;
7.
8.
9. import com.opensymphony.xwork2.ActionSupport;
10.
11. public class FileUploadAction extends ActionSupport{
12.
13. private static final long serialVersionUID = 572146812454l ;
14. private static final int BUFFER_SIZE = 16 * 1024 ;
15.
16. //注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定
17. //所以同时要提供myFileContentType,myFileFileName的set方法
18.
19. private File myFile; //上传文件
20. private String contentType;//上传文件类型
21. private String fileName; //上传文件名
22. private String imageFileName;
23. private String caption;//文件说明,与页面属性绑定
24.
25. public void setMyFileContentType(String contentType) {
26. System.out.println("contentType : " + contentType);
27. this .contentType = contentType;
28. }
29.
30. public void setMyFileFileName(String fileName) {
31. System.out.println("FileName : " + fileName);
32. this .fileName = fileName;
33. }
34.
35. public void setMyFile(File myFile) {
36. this .myFile = myFile;
37. }
38.
39. public String getImageFileName() {
40. return imageFileName;
41. }
42.
43. public String getCaption() {
44. return caption;
45. }
46.
47. public void setCaption(String caption) {
48. this .caption = caption;
49. }
50.
51. private static void copy(File src, File dst) {
52. try {
53. InputStream in = null ;
54. OutputStream out = null ;
55. try {
56. in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
57. out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
58. byte [] buffer = new byte [BUFFER_SIZE];
59. while (in.read(buffer) > 0 ) {
60. out.write(buffer);
61. }
62. } finally {
63. if ( null != in) {
64. in.close();
65. }
66. if ( null != out) {
67. out.close();
68. }
69. }
70. } catch (Exception e) {
71. e.printStackTrace();
72. }
73. }
74.
75. private static String getExtention(String fileName) {
76. int pos = fileName.lastIndexOf(".");
77. return fileName.substring(pos);
78. }
79.
80. @Override
81. public String execute() {
82. imageFileName = new Date().getTime() + getExtention(fileName);
83. File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages" ) + "/" + imageFileName);
84. copy(myFile, imageFile);
85. return SUCCESS;
86. }
87. }
package com.chris;
import java.io.*;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private static final long serialVersionUID = 572146812454l ;
private static final int BUFFER_SIZE = 16 * 1024 ;
//注意,文件上传时<s:file/>同时与myFile,myFileContentType,myFileFileName绑定
//所以同时要提供myFileContentType,myFileFileName的set方法
private File myFile; //上传文件
private String contentType;//上传文件类型
private String fileName; //上传文件名
private String imageFileName;
private String caption;//文件说明,与页面属性绑定
public void setMyFileContentType(String contentType) {
System.out.println("contentType : " + contentType);
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
System.out.println("FileName : " + fileName);
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() {
imageFileName = new Date().getTime() + getExtention(fileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages" ) + "/" + imageFileName);
copy(myFile, imageFile);
return SUCCESS;
}
}
注:此时仅为方便实现Action所以继承ActionSupport,并Overrider execute()方法
在struts2中任何一个POJO都可以作为Action
4.struts.xml清单如下:
Java代码
1. <?xml version="1.0" encoding="UTF-8" ?>
2. <!DOCTYPE struts PUBLIC
3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4. "http://struts.apache.org/dtds/struts-2.0.dtd">
5. <struts>
6. <package name="example" namespace="/" extends="struts-default">
7. <action name="fileUpload" class="com.chris.FileUploadAction">
8. <interceptor-ref name="fileUploadStack"/>
9. <result>/ShowUpload.jsp</result>
10. </action>
11. </package>
12. </struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="example" namespace="/" extends="struts-default">
<action name="fileUpload" class="com.chris.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<result>/ShowUpload.jsp</result>
</action>
</package>
</struts>
5.web.xml清单如下:
Java代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.4"
3. xmlns="http://java.sun.com/xml/ns/j2ee"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7. <filter >
8. <filter-name > struts-cleanup </filter-name >
9. <filter-class >
10. org.apache.struts2.dispatcher.ActionContextCleanUp
11. </filter-class >
12. </filter >
13. <filter-mapping >
14. <filter-name > struts-cleanup </filter-name >
15. <url-pattern > /* </url-pattern >
16. </filter-mapping >
17.
18. <filter>
19. <filter-name>struts2</filter-name>
20. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
21. </filter>
22. <filter-mapping>
23. <filter-name>struts2</filter-name>
24. <url-pattern>/*</url-pattern>
25. </filter-mapping>
26. <welcome-file-list>
27. <welcome-file>Index.jsp</welcome-file>
28. </welcome-file-list>
29.
30. </web-app>
- hollowFile.rar (3.2 MB)
- 下载次数: 16
发表评论
文章已被作者锁定,不允许评论。
相关推荐
Struts2是一款非常流行的Java Web框架,用于构建企业级应用。然而,随着时间的推移,Struts2在安全方面暴露出了一些重要的漏洞,这给使用该框架的系统带来了潜在的安全风险。"Struts2漏洞检查工具Struts2.2019.V2.3...
Struts2是一个强大的Java EE应用程序框架,主要用于构建企业级的Web应用。它的核心是MVC(Model-View-Controller)设计模式,可以帮助开发者组织代码,提高开发效率,并且提供了丰富的特性来支持表单验证、国际化、...
Struts2 项目开发 Struts2 是一个基于 Java Web 的框架,广泛应用于 Web 应用程序的开发。下面将从 Struts2 项目开发的角度,详细介绍 Struts2 框架的应用、开发流程、技术架构、实践经验等方面的知识点。 项目...
### Struts2核心知识点解析 #### 一、Struts2框架概述 - **定义与特点**:Struts2是一款基于MVC(Model-View-Controller)设计模式的Java Web应用程序框架,它继承了Struts1的优点,同时在设计上更加灵活、易用,...
struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...
该工具的打开路径为:\Struts2VulsTools-2.3.20190927\Test\bin\Release\Text.exe 2019-09-25: 优化部分EXP在部分情况下被WAF拦截的问题,提高检测成功率,优化自定义上传路径exp,文件所在目录不存在时自动创建...
Struts2是一个强大的Java web应用程序开发框架,它基于Model-View-Controller(MVC)设计模式,旨在简化创建用户交互式、数据驱动的web应用的过程。这个“Struts2接口文档”是开发者的重要参考资料,提供了关于...
Struts2-showcase是一个用于演示和学习Apache Struts2框架功能的开源项目。这个压缩包“struts2-showcase.rar”包含了完整的源代码,旨在帮助开发者深入理解Struts2框架的工作原理及其各种特性。以下是对Struts2和...
从给定的文件信息来看,标题“struts2中文学习文档”和描述“struts2的根本webwork2”表明这是一份关于Struts2框架的学习资料,特别强调了Struts2与WebWork2的关系。Struts2是Apache Struts的一个版本,它是一个用于...
"struts2 jar"文件包含了Struts2框架的核心库,可能包括struts2-core、struts2-convention、struts2-json-plugin等依赖,这些是开发Struts2应用必不可少的组件。 "Struts2"可能是项目实例代码,包括Action类、视图...
Struts2是一个非常著名的Java Web开发框架,由Apache软件基金会维护。它基于MVC(Model-View-Controller)设计模式,极大地简化了构建基于Java EE的Web应用程序的过程。本资源包含"struts2所有jar包程序文件",是...
-- 为修复struts2 s2-016、s2-017漏洞,重写DefaultActionMapper --> <bean type="org.apache.struts2.dispatcher.mapper.ActionMapper" name="myDefaultActionMapper" class=...
Struts2是一个强大的Java web开发框架,用于构建可维护、可扩展且结构良好的应用程序。它在MVC(Model-View-Controller)设计模式的基础上提供了一种实现方式,使得开发者能够更方便地处理用户请求,控制业务逻辑,...
Struts2是Apache软件基金会下的一个开源框架,主要用于构建企业级的Java web应用程序。张龙圣思园的Struts2学习笔记,无疑为Java开发者提供了一份宝贵的参考资料,它可能涵盖了Struts2的基础概念、核心组件、配置...
struts2 chm 程序包 org.apache.struts2 接口概要 接口 说明 StrutsStatics Constants used by Struts. 类概要 类 说明 RequestUtils Request handling utility class. ServletActionContext Web-specific ...
包含struts2-core-2.5.10.1.jar,struts2-jfreechart-plugin-2.5.10.1.jar,struts2-json-plugin-2.5.10.1.jar,struts2-junit-plugin-2.5.10.1.jar,struts2-bean-validation-plugin-2.5.10.1.jar,struts2-cdi-...
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全...
Struts 2是一款基于Java的开源MVC框架,它在Web应用开发中广泛使用,但同时也因其复杂的架构和历史遗留问题,成为了网络安全的焦点。这个标题提到的是一个全面的Struts 2漏洞检测工具,旨在帮助开发者和安全专家识别...
Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试...