欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP开发之Struts2实现下载功能的实例

程序员文章站 2022-06-11 14:23:38
jsp开发之struts2实现下载功能的实例 1、action 类 package com.favccxx.strutsdemo; import...

jsp开发之struts2实现下载功能的实例

1、action 类

package com.favccxx.strutsdemo; 
 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.inputstream; 
 
import com.opensymphony.xwork2.actionsupport; 
 
public class downloadaction extends actionsupport { 
 
   
  private static final long serialversionuid = -7758584364932001138l; 
   
  private inputstream inputstream; 
 
  @override 
  public string execute() throws exception { 
     inputstream = new fileinputstream(new file("d:\\芒果豆.jpg")); 
     return success; 
  } 
 
  public inputstream getinputstream() { 
    return inputstream; 
  } 
 
  public void setinputstream(inputstream inputstream) { 
    this.inputstream = inputstream; 
  } 
   
   
 
} 

2、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> 
 
  <constant name="struts.enable.dynamicmethodinvocation" value="false" /> 
  <constant name="struts.devmode" value="true" /> 
 
  <package name="default" namespace="/" extends="struts-default"> 
 
    <default-action-ref name="index" /> 
 
    <global-results> 
      <result name="error">/error.jsp</result> 
    </global-results> 
 
    <global-exception-mappings> 
      <exception-mapping exception="java.lang.exception" result="error"/> 
    </global-exception-mappings> 
 
    <action name="index"> 
      <result type="redirectaction"> 
        <param name="actionname">helloworld</param> 
        <param name="namespace">/example</param> 
      </result> 
    </action> 
     
    <action name="download" class="com.favccxx.strutsdemo.downloadaction"> 
      <result name="success" type="stream"> 
        <param name="contenttype">application/octet-stream</param> 
        <param name="inputname">inputstream</param> 
        <param name="contentdisposition">attachment;filename="mongo.jpg"</param> 
        <param name="buffersize">1024</param> 
      </result> 
    </action> 
  </package> 
 
</struts> 

3、web.xml

<?xml version="1.0" encoding="utf-8"?> 
<web-app id="webapp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 
  xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
 
  <display-name>struts blank</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.html</welcome-file> 
  </welcome-file-list> 
 
</web-app> 

4、jsp

<a href="/download" rel="external nofollow" >无注解下载</a> 

以上就是jsp实现下载功能的实例代码,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!