java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))
1.自定义拦截器:


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>
<package name="inteceptor" extends="struts-default"> <interceptors>
<!-- 配置自定义拦截器 -->
<interceptor name="myInter" class="cn.zzsxt.interceptor.MyInterceptor"></interceptor>
<interceptor name="permission" class="cn.zzsxt.interceptor.PermissionInterceptor"></interceptor> <!-- 自定义拦截器栈 -->
<interceptor-stack name="myStack">
<!-- 引入自定义拦截器 -->
<interceptor-ref name="myInter"></interceptor-ref>
<!-- 引入timer拦截器 -->
<interceptor-ref name="timer"></interceptor-ref>
<!-- 引入默认拦截器栈 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack> <!-- 自定义访问权限的拦截器栈 -->
<interceptor-stack name="permissionStack">
<interceptor-ref name="permission"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors> <action name="timerAction" class="cn.zzsxt.action.TimerAction">
<result name="success">/success.jsp</result>
<interceptor-ref name="myStack"></interceptor-ref>
<interceptor-ref name="permissionStack"></interceptor-ref>
</action>
<action name="userAction" class="cn.zzsxt.action.UserAction">
<result name="success" type="redirectAction">homeAction</result>
<result name="login">/login.jsp</result>
<interceptor-ref name="permissionStack"></interceptor-ref>
</action>
<action name="homeAction" class="cn.zzsxt.action.HomeAction">
<result name="success">/list.jsp</result>
<result name="login" type="redirect">/login.jsp</result>
<!-- 引入拦截器栈 -->
<interceptor-ref name="permissionStack"></interceptor-ref>
</action>
</package>
</struts>
2.token令牌:
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> <!-- 上传文件最大大小,默认为2097152字节(2M) -->
<constant name="struts.multipart.maxSize" value="209715200"></constant> <!-- 设置编码集 request.setCharacterEncoding("UTF-8")-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="tokenDemo" extends="struts-default">
<action name="user-*" class="cn.zzsxt.action.UserAction" method="{1}">
<result name="success">/index.jsp</result>
<!-- token验证失败后跳转的结果视图 -->
<result name="invalid.token">/error.jsp</result>
<!-- 引入token拦截器 -->
<interceptor-ref name="token"></interceptor-ref>
<!-- 引入默认拦截器栈 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
add.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>My JSP 'index.jsp' starting page</title>
</head> <body>
<form action="user-doAdd.action" method="post">
<!-- token令牌 -->
<s:token></s:token>
用户编号:<input type="text" name="user.id"><br>
用户名称:<input type="text" name="user.userName"><br>
密码:<input type="text" name="user.password"><br>
<input type="submit" value="新增"/>
</form>
</body>
</html>
action:
package cn.zzsxt.action;
import com.opensymphony.xwork2.ActionSupport;
import cn.zzsxt.entity.User;
public class UserAction extends ActionSupport{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String doAdd() throws Exception {
System.out.println("执行了用户新增,新增了:"+user);
return this.SUCCESS;
}
}
3.文件上传和下载(单/多):
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> <!-- 上传文件最大大小,默认为2097152字节(2M) -->
<constant name="struts.multipart.maxSize" value="209715200"></constant>
<!-- 设置编码集 request.setCharacterEncoding("UTF-8")-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant> <package name="upload" extends="struts-default">
<action name="singleUpload" class="cn.zzsxt.action.SingleUploadAction">
<!-- result中name属性缺省(默认)为success -->
<result>/success.jsp</result>
</action>
<action name="mutilUpload" class="cn.zzsxt.action.MutilUploadAction">
<!-- result中name属性缺省(默认)为success -->
<result>/success.jsp</result>
</action> <action name="download" class="cn.zzsxt.action.DownloadAction">
<result type="stream">
<!-- inputName="输入流的名称",回掉action中getInputStream()方法获取输入流 -->
<param name="inputName">inputStream</param>
<!--contentType="文件类型" application/octet-stream代表对下载类型不限制 -->
<param name="contentType">application/octet-stream</param>
<!-- attachment:以附件方式保存 filename="保存的文件名称" ${fileName}获取action中fileName属性的值 -->
<param name="contentDisposition">attachment;filename=${fileName}</param>
</result>
</action>
</package>
</struts>
SingleUploadAction:
package cn.zzsxt.action; import java.io.File; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class SingleUploadAction extends ActionSupport{
private File upload;//文件
private String uploadFileName;//文件名称:属性名+FileName
private String uploadContentType;//文件类型:属性名+ContentType public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} /**
* 执行文件上次
*/
@Override
public String execute() throws Exception {
System.out.println("文件名称:"+uploadFileName);
System.out.println("文件类型:"+uploadContentType);
ServletContext servletContext = ServletActionContext.getServletContext();
String realPath = servletContext.getRealPath("/upload");//获取文件上次的真实目录
File uploadDir = new File(realPath);
if(!uploadDir.exists()){
uploadDir.mkdirs();//创建目录
}
//上传
FileUtils.copyFile(upload, new File(uploadDir,uploadFileName));
return this.SUCCESS;
}
}
SingleUpload.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>
</head> <body>
<form action="singleUpload.action" method="post" enctype="multipart/form-data">
文件:<input type="file" name="upload"></br>
<input type="submit" value="上传"/>
</form>
</body>
</html>
MutilUploadAction:
package cn.zzsxt.action; import java.io.File; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class MutilUploadAction extends ActionSupport{
private File[] upload;//文件
private String[] uploadFileName;//文件名称:属性名+FileName
private String[] uploadContentType;//文件类型:属性名+ContentType
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
} /**
* 执行文件上次
*/
@Override
public String execute() throws Exception {
System.out.println("文件名称:"+uploadFileName);
System.out.println("文件类型:"+uploadContentType);
ServletContext servletContext = ServletActionContext.getServletContext();
String realPath = servletContext.getRealPath("/upload");//获取文件上次的真实目录
File uploadDir = new File(realPath);
if(!uploadDir.exists()){
uploadDir.mkdirs();//创建目录
}
//遍历待上传的文件
for(int i=0;i<upload.length;i++){
//上传
FileUtils.copyFile(upload[i], new File(uploadDir,uploadFileName[i]));
}
return this.SUCCESS;
}
}
MutilUpload.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>
</head> <body>
<form action="mutilUpload.action" method="post" enctype="multipart/form-data">
文件1:<input type="file" name="upload"></br>
文件2:<input type="file" name="upload"></br>
文件3:<input type="file" name="upload"></br>
<input type="submit" value="上传"/>
</form>
</body>
</html>
DownloadAction:
package cn.zzsxt.action; import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport{
private String fileName;//接受要下载的文件名称
private InputStream inputStream; public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public InputStream getInputStream() {
return inputStream;
} public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
} /**
* 文件下载
*/
@Override
public String execute() throws Exception {
//获取服务器端保存文件的目录
ServletContext servletContext = ServletActionContext.getServletContext();
String realPath = servletContext.getRealPath("/upload");//获取文件上传的真实目录
File file = new File(realPath,fileName);
inputStream = new FileInputStream(file); //将文件转为输入流
//解决下载时中文名称乱码问题
String str = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
fileName = str;
return this.SUCCESS;
}
}
Download.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>
</head> <body>
<a href="download.action?fileName=1.txt">1.txt</a><br>
<a href="download.action?fileName=mystruts.png">mystruts.png</a><br>
<a href="download.action?fileName=Log4J详解.docx">Log4J详解.docx</a><br>
</body>
</html>
java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))的更多相关文章
- java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息
1.java使用Jsch实现远程操作linux服务器进行文件上传.下载,删除和显示目录信息. 参考链接:https://www.cnblogs.com/longyg/archive/2012/06/2 ...
- java框架篇---struts之文件上传和下载
Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...
- Java Struts文件上传和下载详解
Struts2文件上传 Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传.上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的 ...
- java web学习总结(二十四) -------------------Servlet文件上传和下载的实现
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- java文件上传和下载
简介 文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...
- java的文件上传和下载 抄袭别人的.在底部有说明.
=======后续 这里采用的是输出流的方式,我电脑装的是windows系统,测试没有问题,但是当把项目放到Linux系统上跑时,就会出现保存位置错误的情况, 指定的路径就会被当做文件名的一部分保存了 ...
- common-fileupload组件实现java文件上传和下载
简介:文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...
- 使用.NET框架、Web service实现Android的文件上传(二)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYUAAAKpCAIAAADcx6fPAAAgAElEQVR4nOydd1hT5+LHg1attbfr1t ...
- java代码实现ftp服务器的文件上传和下载
java代码实现文件上传到ftp服务器: 1:ftp服务器安装: 2:ftp服务器的配置: 启动成功: 2:客户端:代码实现文件的上传与下载: 1:依赖jar包: 2:sftpTools 工具类: ...
随机推荐
- docker查看容器IP地址
docker inspect 容器名称或 id 命令:docker inspect redismaster 结果:
- Acwing-120-防线(二分,前缀和)
链接: https://www.acwing.com/problem/content/122/ 题意: 达达学习数学竞赛的时候受尽了同仁们的鄙视,终于有一天......受尽屈辱的达达黑化成为了黑暗英雄 ...
- 32.把数组排成最小的数(python)
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. # -*- ...
- python selenium 执行完毕关闭chromedriver进程
#OS是我们操作dos系统时需要引入的库 import os #杀死这个chromedriver进程,因为每次启动都会打开,所以需要kill,这里用的chrome浏览器,打开方式时chromedriv ...
- jquery enabled选择器 语法
jquery enabled选择器 语法 作用::enabled 选择器选取所有启用的表单元素.大理石平台精度等级 语法:$(":enabled") jquery enabled选 ...
- #415 Div2 Problem C Do you want a data? (math && 前后缀和 && 快速幂)
题意: 首先定义集合的F值为 这个集合里面最大值和最小值的差. 现给出一个拥有n个数的集合(没有相同的元素), 要求求出这个集合内所有子集的F的值的和.例如: {4.7}这个集合里面有子集{4}.{ ...
- Python字典里的5个黑魔法
Python里面有3大数据结构:列表,字典和集合.字典是常用的数据结构,里面有一些重要的技巧用法,我把这些都整理到一起,熟练掌握这些技巧之后,对自己的功力大有帮助. 1.字典的排序: 用万金油sort ...
- ubuntu16.04和vim的配置与美化
目录 ubuntu16.04配置 ubuntu16.04 美化 vim配置 vim美化 ubuntu16.04配置 安装vim sudo apt-get install vim-gnome 换源 su ...
- 「TJOI2019」唱、跳、rap 和篮球
题目链接 题目分析 据说这是一道生成函数题 看到限制条件,我们首先想到的就是对有多少组讨论cxk的人进行容斥.然后就是求剩下的人随便放有多少种方法了.考虑现在每种剩\(a,b,c,d\)人,还需要排\ ...
- JavaScript插件制作-tab选项卡
JavaScript插件制作练习-鼠标划过选项卡切换图片 <!DOCTYPE html> <html> <head> <meta charset=" ...