前一天,上传了关于struts2文件的下载方式,今天正好补上关于struts2文件下载的方式。
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 ;
}