Struts2完成文件的下载

Struts2完成文件的下载

10级学员 郞志宋利兴课堂笔记

第一步:

编写jsp页面添加下载的超链接

[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2.   
  3. <%@ taglib prefix="s" uri="/struts-tags" %>  
  4.   
  5. <%  
  6. String path = request.getContextPath();  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8. %>  
  9.   
  10. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  11. <html>  
  12.   <head>  
  13.     <base href="<%=basePath%>">  
  14.     <title>My JSP 'down.jsp' starting page</title>  
  15.     <meta http-equiv="pragma" content="no-cache">  
  16.     <meta http-equiv="cache-control" content="no-cache">  
  17.     <meta http-equiv="expires" content="0">      
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19.     <meta http-equiv="description" content="This is my page">  
  20.   
  21.     <!-- 
  22.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  23.     -->  
  24.   </head>  
  25.   <body>  
  26.     <h1>文件下载</h1>  
  27.     <!-- 下载链接 -->  
  28.     <s:a action="down.action">download</s:a>  
  29.   </body>  
  30. </html>  

第二步:编写下载文件的Action文件

[java]  view plain copy
  1. package cn.csdn.hr.up.action;  
  2.   
  3. import java.io.InputStream;  
  4. import org.apache.struts2.ServletActionContext;  
  5. import com.opensymphony.xwork2.ActionSupport;  
  6. public class DownAction extends ActionSupport {  
  7.   
  8.      private static final long serialVersionUID = 1L;  
  9.     private String inputPath;  
  10.     public String getInputPath() {  
  11.         return inputPath;  
  12.     }  
  13.     public void setInputPath(String inputPath) {  
  14.         this.inputPath = inputPath;  
  15.     }  
  16.     /* 
  17.      * 下载用的Action应该返回一个InputStream实例,该方法对应在result里的inputName属性为 
  18.  
  19.      * getDownloadFile 
  20.  
  21.      */  
  22.   
  23.     public InputStream getDownloadFile()  
  24.   
  25.     {  
  26.         return ServletActionContext.getServletContext().getResourceAsStream(  
  27.         "/upload/Struts2.ppt");  
  28.     }  
  29.   
  30.     @Override  
  31.     public String execute() throws Exception  
  32.     {  
  33.         return SUCCESS;  
  34.   
  35.     }  
  36. }  

第三步:

在struts2配置文件中定义action文件

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE struts PUBLIC  
  4.   
  5.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  6.   
  7.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  8.   
  9. <struts>  
  10.   
  11.     <package name="down" extends="struts-default">  
  12.   
  13.         <action name="down" class="cn.csdn.hr.up.action.DownAction">  
  14.   
  15.             <!-- 配置结果类型为stream的结果 -->  
  16.   
  17.             <result name="success" type="stream">  
  18.   
  19.                 <!-- 指定被下载文件的文件类型 -->  
  20.   
  21.                 <param name="contentType">application/vnd.ms-powerpoint</param>  
  22.   
  23.                 <!-- 指定下载文件的文件位置 -->  
  24.   
  25.                 <param name="contentDisposition">filename="Struts2.ppt"</param>  
  26.   
  27.                 <param name="inputName">downloadFile</param>  
  28.   
  29.             </result>  
  30.   
  31.         </action>  
  32.   
  33.     </package>    
  34.   
  35. </struts>  

测试点击download链接

会弹出下载窗口:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值