本测试有两个模块,一个是文件上上传,一个是文件下载,文件下载的时候会检查是否足有权限,如果没有,就会转发到登录页面,如果有权限,就会直接启动下载程序,给浏览器一个输出流。

下面直接上我的代码:

登录表单


<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=&quot;download.file&quot;
</param>
</result> <result name="login">/login.jsp</result>
</action></package></struts>

版权声明:本文为博主原创文章,未经博主允许不得转载。

使用struts2进行文件下载以及下载权限控制的例子的更多相关文章

  1. Struts2使用拦截器完成权限控制示例

    http://aumy2008.iteye.com/blog/146952 Struts2使用拦截器完成权限控制示例 示例需求:    要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源:否 ...

  2. Confluence实现附件下载权限的控制

    背景: 公司为了方便的管理过程文档,搭建了一个Confluence服务器,版本6.9.在使用过程中,需要按照用户对空间中上传的附件进行下载权限控制. 解决过程及处理方案: 一.Confluence中导 ...

  3. 在struts2.3.4.1中使用注解、反射、拦截器实现基于方法的权限控制

    权限控制是每一个系统都应该有的一个功能,有些只需要简单控制一下就可以了,然而有些却需要进行更加深入和细致的权限控制,尤其是对于一些MIS类系统,基于方法的权限控制就更加重要了. 用反射和自定义注解来实 ...

  4. struts2拦截器加自定义注解实现权限控制

    https://blog.csdn.net/paul342/article/details/51436565 今天结合Java的Annotation和Struts2进行注解拦截器权限控制. 功能需求: ...

  5. yii2 rbac权限控制之菜单menu详细教程

    作者:白狼 出处:http://www.manks.top/article/yii2_rbac_menu本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...

  6. yii2权限控制rbac之菜单menu最详细教程

    前面我们在博文 yii2搭建完美后台并实现rbac权限控制实例教程中完美实现了yii2的后台搭建和rbac权限控制,如果你还没有实现,请先看上文再回来参考本文,因为本文是在上文的基础上进行完善和补充. ...

  7. Struts2基础-4-2 -struts拦截器实现权限控制案例+ 模型驱动处理请求参数 + Action方法动态调用

    1.新建项目,添加jar包到WEB-INF目录下的lib文件夹,并添加到builde path里面 整体目录结构如下 2.新建web.xml,添加struts2核心过滤器,和默认首页 <?xml ...

  8. Struts2中基于Annotation的细粒度权限控制

    Struts2中基于Annotation的细粒度权限控制 2009-10-19 14:25:53|  分类: Struts2 |  标签: |字号大中小 订阅     权限控制是保护系统安全运行很重要 ...

  9. Struts2使用Interceptor实现权限控制的应用实例详解

    Struts2使用Interceptor实现权限控制的应用实例详解 拦截器:是Struts2框架的核心,重点之重.因此,对于我们要向彻底学好Struts2.0.读源码和使用拦截器是必不可少的.少说了. ...

随机推荐

  1. vs2008 发布网站时丢失文件问题

    右键指定的文件->属性, 将生成操作更改成为"内容"就可以了.

  2. Java进阶学习:log4j的学习和使用

    Java进阶学习——log4j的学习和使用 简介Loj4j Log4j的组成 Log4j主要由三大组组件构成: Logger: 负责生成日志,并能够对日志信息进行分类筛选,通俗的讲就是决定什么日志信息 ...

  3. Elatsicsearch分片和副本相关知识

    1.分片和副本 1.1什么是分片 简单来讲就是咱们在ES中所有数据的文件块,也是数据的最小单元块,整个ES集群的核心就是对所有分片的分布.索引.负载.路由等达到惊人的速度. 分片是把索引数据切分成多个 ...

  4. 每天一个Linux命令(34)grep命令

          grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具 ...

  5. css3图片过滤效果

    在线演示 本地下载

  6. 使用 Apache poi 导入Excel

    本文主要记录Excel导入及模板下载,遇到的问题及注意事项. 第一节:Excel导入   1.如何获取Excel中的最大行,也就是最后一行? 2.如何获取有效行?有效行的定义是每一行记录中每一列中值都 ...

  7. Monkey for Mac 环境配置

    Monkey for Mac环境配置步骤 java环境配置, 直接去官网找对应jdk就可以了 Android  AdtBundle环境配置 1) 下载地址: http://www.jianshu.co ...

  8. 系统安装记录 install OS

    上个系统很乱,基本系统是lfs7.7,上面应用都是基于lfs7.9,基本系统是才接触lfs时搭建的,打包保存后一直没怎么使用过,到lfs7.10快出来的时候有段时间有空就拿出来跑了一下,安装了一些软件 ...

  9. Spring Boot- 设置拦截打印日志

    import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspe ...

  10. iOS App被拒原因以及解决方案总结。

    Guideline 1.2 - Safety - User Generated Content Your app enables the display of user-generated conte ...