使用struts2进行文件下载以及下载权限控制的例子
本测试有两个模块,一个是文件上上传,一个是文件下载,文件下载的时候会检查是否足有权限,如果没有,就会转发到登录页面,如果有权限,就会直接启动下载程序,给浏览器一个输出流。
下面直接上我的代码:
登录表单
<body>
<form id="form1" name="form1" method="post" action="login.action">
<p>
<label for="textfield">账号</label> <input type="text" name="username"
id="textfield" />
</p>
<p>
<label for="textfield2">密码</label> <input type="text" name="password"
id="textfield2" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" /> <br />
</p>
</form>
</body>
上传表单:
<body>
<form action="upload.action" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>
<label for="fileField"></label>
<input type="file" name="fileobj" id="fileField" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
<br />
</p>
</form>
<a href="showpic.action">查看上传照片</a>
</body>
查看文件列表:
<table cellspacing="5" align="center">
<tr>
<s:iterator value="#request.photoList" id="photo" status="stu">
<td><a
href="downpic.action?inputFile=<s:property value="photo"/>"> <img
src='images/<s:property value="photo"/>' width="400" height="420">
</a>
</td>
</tr>
<tr>
</s:iterator>
</tr>
</table>
下面是action部分:
package com.updown.Action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private File fileobj;
private String fileobjFileName;
private String fileobjContentType;
/**
* @return
*/
public String execute() throws Exception {
InputStream is = new FileInputStream(fileobj);
String photoPath = ServletActionContext.getServletContext()
.getRealPath("/images");
File filePhotoPath = new File(photoPath);
if (!filePhotoPath.isDirectory()) {
filePhotoPath.mkdir();
}
String extension = FilenameUtils.getExtension(this.getFileobjFileName());
String filename = UUID.randomUUID().toString() + "." + extension;
File tofile = new File(photoPath, filename);
OutputStream os = new FileOutputStream(tofile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();
return SUCCESS;
}
public File getFileobj() {
return fileobj;
}
public void setFileobj(File fileobj) {
this.fileobj = fileobj;
}
public String getFileobjFileName() {
return fileobjFileName;
}
public void setFileobjFileName(String fileobjFileName) {
this.fileobjFileName = fileobjFileName;
}
public String getFileobjContentType() {
return fileobjContentType;
}
public void setFileobjContentType(String fileobjContentType) {
this.fileobjContentType = fileobjContentType;
}
}
package com.updown.Action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private String username;
private String password;
/**
* @return
*/
public String execute() {
// TODO Auto-generated method stub
if (username.equals("admin")&&password.equals("admin")){
ServletActionContext.getRequest().getSession().putValue("user", "admin");
return SUCCESS;
}
return INPUT;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.updown.Action;
import java.io.File;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ShowPicAction extends ActionSupport {
private String inputFile;
private InputStream targetFile;
public void setTargetFile(InputStream targetFile) {
this.targetFile = targetFile;
}
public InputStream getTargetFile(){
return ServletActionContext.getServletContext().getResourceAsStream("/images/"+inputFile);
}
/**
* @return
*/
public String execute() {
String photoPath = ServletActionContext.getServletContext()
.getRealPath("/images");
File fphotoPath = new File(photoPath);
String[] photoList = fphotoPath.list();
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("photoList", photoList);
return SUCCESS;
}
public String downfile(){
String user = (String) ServletActionContext.getRequest().getSession().getAttribute("user");
if (user!=null&&user.equals("admin"))
return SUCCESS;
return LOGIN;
}
public String getInputFile() {
return inputFile;
}
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
}
struts-xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<default-action-ref name="download"></default-action-ref>
<action name="login" class="com.updown.Action.UserAction">
<result name="success">/upload.jsp</result>
<result name="input">/login.jsp</result>
</action>
<action name="upload" class="com.updown.Action.UploadAction">
<result name="success">/upload.jsp</result>
</action>
<action name="showpic"
class="com.updown.Action.ShowPicAction">
<result name="success">/index.jsp</result>
</action>
<action name="downpic" class="com.updown.Action.ShowPicAction"
method="downfile">
<result name="success" type="stream">
<param name="contentType">
application/octet-stream
</param>
<param name="inputName">targetFile</param>
<param name="bufferSize">4096</param>
<param name="contentDisposition">
filename="download.file"
</param>
</result>
<result name="login">/login.jsp</result>
</action></package></struts>
版权声明:本文为博主原创文章,未经博主允许不得转载。
使用struts2进行文件下载以及下载权限控制的例子的更多相关文章
- Struts2使用拦截器完成权限控制示例
http://aumy2008.iteye.com/blog/146952 Struts2使用拦截器完成权限控制示例 示例需求: 要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源:否 ...
- Confluence实现附件下载权限的控制
背景: 公司为了方便的管理过程文档,搭建了一个Confluence服务器,版本6.9.在使用过程中,需要按照用户对空间中上传的附件进行下载权限控制. 解决过程及处理方案: 一.Confluence中导 ...
- 在struts2.3.4.1中使用注解、反射、拦截器实现基于方法的权限控制
权限控制是每一个系统都应该有的一个功能,有些只需要简单控制一下就可以了,然而有些却需要进行更加深入和细致的权限控制,尤其是对于一些MIS类系统,基于方法的权限控制就更加重要了. 用反射和自定义注解来实 ...
- struts2拦截器加自定义注解实现权限控制
https://blog.csdn.net/paul342/article/details/51436565 今天结合Java的Annotation和Struts2进行注解拦截器权限控制. 功能需求: ...
- yii2 rbac权限控制之菜单menu详细教程
作者:白狼 出处:http://www.manks.top/article/yii2_rbac_menu本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...
- yii2权限控制rbac之菜单menu最详细教程
前面我们在博文 yii2搭建完美后台并实现rbac权限控制实例教程中完美实现了yii2的后台搭建和rbac权限控制,如果你还没有实现,请先看上文再回来参考本文,因为本文是在上文的基础上进行完善和补充. ...
- Struts2基础-4-2 -struts拦截器实现权限控制案例+ 模型驱动处理请求参数 + Action方法动态调用
1.新建项目,添加jar包到WEB-INF目录下的lib文件夹,并添加到builde path里面 整体目录结构如下 2.新建web.xml,添加struts2核心过滤器,和默认首页 <?xml ...
- Struts2中基于Annotation的细粒度权限控制
Struts2中基于Annotation的细粒度权限控制 2009-10-19 14:25:53| 分类: Struts2 | 标签: |字号大中小 订阅 权限控制是保护系统安全运行很重要 ...
- Struts2使用Interceptor实现权限控制的应用实例详解
Struts2使用Interceptor实现权限控制的应用实例详解 拦截器:是Struts2框架的核心,重点之重.因此,对于我们要向彻底学好Struts2.0.读源码和使用拦截器是必不可少的.少说了. ...
随机推荐
- JVM虚拟机调参
一.堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为 ...
- iOS 图文混排 链接 可点击
对于这个话题 我想到 1 第一个解决方法就是使用 webView 比较经典 把所有复杂工作都交给控件本身去处理了, 但是好像好多需要自定义的地方 没法从 webView获得响应回调 :(估计也可以实 ...
- swift 全局常量 && 全局变量的写法
在OC里面 如果 想设置一个全局常量 很简单 使用简单宏定义 就搞定了 例如: #define WEBAPIBASEURL @"http://www.baidu.com/" ...
- 【leetcode刷题笔记】Anagrams
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Vue mixins extends extend components
mixins 调用方式: mixins: [mixin1, mixin2] 是对父组件的扩充,包括methods.components.directive等... 触发钩子函数时,先调用mixins的 ...
- 20145210姚思羽《网络对抗》——shellcode注入& Return-to-libc攻击深入
20145210姚思羽<网络对抗>shellcode注入&Return-to-libc攻击深入 shellcode基础知识 Shellcode是一段代码,作为数据发送给受攻击服务器 ...
- [java]Arrays.copyOf() VS System.arrayCopy()
If we want to copy an array, we can use either System.arraycopy() or Arrays.copyOf(). In this post, ...
- JSP<jsp:forward>与<%@ include%>
JSP<jsp:forward>与<%@ include%><jsp:include> <jsp:forward file="forwardTo_p ...
- CSS重置 reset.css
1. [文件] reset.css ~/*------------------------------------------* site:ifnoif.net* Style author:ifnoi ...
- Spring MVC的工作原理和机制
Spring MVC的工作原理和机制 参考: springMVC 的工作原理和机制 - 孤鸿子 - 博客园https://www.cnblogs.com/zbf1214/p/5265117.html ...