struts2——文件的下载以及在Action中获取请求参数的方式
2017-05-10 16:13
519 查看
前一天,上传了关于struts2文件的下载方式,今天正好补上关于struts2文件下载的方式。
1、开发环境
jdk1.7.0-51
myEcplise10
struts-2.3.32
2.web.xml配置
3.struts.xml配置
4.jsp代码
5.Action代码
到此文件下载的步骤就算完成了,下面说一下关于如何在action中获取请求参数的方式:
方式一:在Action中使用ActionContext得到parameterMap获取参数(本文就是采用的这种方式)
方式一: 将参数作为Action的类属性,利用struts2的OGNL自动填充
在action将要获取参数名设置为action中的私有属性
然后在对应的方法中直接获取就好了(get和set方法省略)
方式三:在Action中取得HttpServletRequest对象,使用request.getParameter获取参数
1、开发环境
jdk1.7.0-51
myEcplise10
struts-2.3.32
2.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"> <display-name></display-name> <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>
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> <package name="download" extends="struts-default"> <action name="fileLoad_*" class="com.wh.downLoad.DownLoadAction" method="{1}"> <result type="stream"> <param name="bufferSize">1024*2</param> </result> </action> </package> </struts>
4.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>My JSP 'index.jsp' starting page</title> </head> <body> <a href="download/fileLoad_Load?fileName=append.html">下载</a> </body> </html>
5.Action代码
package com.wh.downLoad; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Map; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class DownLoadAction extends ActionSupport{ private static final long serialVersionUID = 1L; private String contentType ;//文件的类型 private long contentLength ;//流的长度 private String contentDisposition ;// 用于指定文件名的内容配置头值 private InputStream inputStream;//读取文件的输入流 public String Load() throws IOException{ //通过getParameters()方法获取请求的参数集合 Map<String ,Object> map = ActionContext.getContext().getParameters(); //通过对应的key获取相应的vuale[] String [] fName = (String[]) map.get("fileName"); //遍历数组获取对应的value值(本文的值只有一个) String fName1 = fName[0]; contentType = "text/html";//文件下载的类型 contentDisposition = "attachment; filename = "+fName1+""; //获取指定下载文件的路径 ServletContext servletContext = ServletActionContext.getServletContext(); String fileName = servletContext.getRealPath("/files/"+fName1+""); inputStream = new FileInputStream(fileName) ; contentLength = inputStream.available(); return SUCCESS; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public long getContentLength() { return contentLength; } public void setContentLength(long contentLength) { this.contentLength = contentLength; } public String getContentDisposition() { return contentDisposition; } public void setContentDisposition(String contentDisposition) { this.contentDisposition = contentDisposition; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } }
到此文件下载的步骤就算完成了,下面说一下关于如何在action中获取请求参数的方式:
方式一:在Action中使用ActionContext得到parameterMap获取参数(本文就是采用的这种方式)
Map<String ,Object> map = ActionContext.getContext().getParameters(); String [] fName = (String[]) map.get("fileName");
方式一: 将参数作为Action的类属性,利用struts2的OGNL自动填充
在action将要获取参数名设置为action中的私有属性
然后在对应的方法中直接获取就好了(get和set方法省略)
public String Load1() throws IOException{ System.out.println(fileName); String fName1 = this.fileName; contentType = "text/html"; contentDisposition = "attachment; filename = "+fName1+""; ServletContext servletContext = ServletActionContext.getServletContext(); String fileName = servletContext.getRealPath("/files/"+fName1+""); inputStream = new FileInputStream(fileName) ; contentLength = inputStream.available(); return SUCCESS ; }
方式三:在Action中取得HttpServletRequest对象,使用request.getParameter获取参数
public String Load2() throws IOException{ HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); String fName2 = request.getParameter("fileName"); contentType = "text/html"; contentDisposition = "attachment; filename = "+fName2+""; ServletContext servletContext = ServletActionContext.getServletContext(); String fileName = servletContext.getRealPath("/files/"+fName2+""); inputStream = new FileInputStream(fileName) ; contentLength = inputStream.available(); return SUCCESS ; }
相关文章推荐
- 表单中多个请求参数名字相同,服务器到底获取的是哪个请求参数的值,表单的action和get和post提交方式的关系以及提交数据的不同点
- 百度网盘上下载文件,调用api接口的请求方式和参数
- Struts学习_Jquery发送ajax请求以及datatype参数为text/JSON方式
- php curl 请求下载文件,以及同时向接口发送参数和文件
- struts2中通过Action以InputStream的下载文件以及在iOS以及Android端接收的实现方式
- struts2中通过Action以InputStream的下载文件以及在iOS以及Android端接收的实现方式
- jquery多选择动态参数的使用以及ajax异步请求中提交整个form表单中serializeArray()的使用以及后台值得获取方式
- jQuery以Post方式发送请求并获取返回的文件供下载!
- struts2业务控制类(Action)获取页面请求参数的几种方式
- jquery流上传获取文件以及请求参数
- Action获取请求参数的三种方式
- JavaScript 和 JQuery获取URL以及URL中的请求参数
- C# 后台获取请求来源、文件下载
- 通过字节流方式实现文件下载以及其中的编码问题
- SSH与SSM学习之Struts203——主配置文件配置_常量配置_动态方法配置_action书写方式
- springMVC 获取请求参数的几种方式
- Struts1 生成Action请求的几种方式分析
- Java:使用HttpClient进行POST和GET请求以及文件上传和下载
- struts2 文件下载struts.xml param 中name的参数说明
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式