struts2實(shí)現(xiàn)文件下載功能
本文實(shí)例為大家分享了struts2下實(shí)現(xiàn)文件下載功能,供大家參考,具體內(nèi)容如下
下面以實(shí)現(xiàn)一個(gè)圖片下載功能為例:
1. 項(xiàng)目結(jié)構(gòu)

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"> <!-- 設(shè)置struts 2過濾器 --> <filter> <filter-name>struts 2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts 2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 設(shè)置歡迎頁面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- session超時(shí)定義,單位為分鐘 --> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
3.DownloadAction.java
package com.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
//文件路徑
private String path;
//path屬性的getter方法
public String getPath(){
return path;
}
//path屬性的setter方法
public void setPath(String path){
this.path = path;
}
//返回inputstream,文件下載關(guān)鍵方法
public java.io.InputStream getDownloadFile() throws Exception{
InputStream in = ServletActionContext.getServletContext().getResourceAsStream(getPath());
return in;
}
public String execute() throws Exception{
return SUCCESS;
}
}
4.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>
<!-- 配置 Struts 2 應(yīng)用中的常量 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 配置上傳文件的最大容量,struts2默認(rèn)為2M。單位是1B, 1KB=1024B,1M=1024KB,1M=1024*1024B-->
<constant name="struts.multipart.maxSize" value="1048576" />
<!-- 配置本應(yīng)用中的包,繼承 struts-default 包 -->
<package name="FileDownload" namespace="/" extends="struts-default">
<action name="download" class="com.action.DownloadAction">
<!-- 設(shè)置文件路徑的參數(shù),傳到action類文件中去 -->
<!-- <param name="path">\download\a.jpg</param> -->
<!-- 下載文件類型定義,即定義為“stream” -->
<result name="success" type="stream">
<!-- image/jpeg代表JPG圖片 -->
<param name="contentType">image/jpeg</param>
<!-- 下載文件處理方法 -->
<param name="contentDisposition">
<!-- attachment表示附件方式,即下載時(shí)打開保存對(duì)話窗,filename表示下載時(shí)顯示的保存時(shí)的文件名 -->
<!-- 如果不寫attachment;或者是寫的是inline; 則表示內(nèi)聯(lián),即會(huì)在瀏覽器中嘗試打開下載的文件,而不是下載-->
attachment;filename="a.jpg"
</param>
<!-- 下載文件輸出流定義 -->
<!-- 這里的inputName元素所對(duì)應(yīng)的value值downloadFile,在action中一定要有對(duì)應(yīng)的getDownloadFile()方法 -->
<param name="inputName">downloadFile</param>
<!-- 下載緩沖區(qū)的大小 -->
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
5.index.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%>" rel="external nofollow" > <title>首頁</title> </head> <body> <center> 歡迎來到首頁,點(diǎn)下面鏈接去下載一個(gè)文件<br /> <a href="download.action?path=<%=" rel="external nofollow" ./download/a.jpg" %>">下載</a> </center> </body> </html>
6.文件路徑
項(xiàng)目中要提前建立好download目錄,和里面要存在有a.jpg文件,否則,下載會(huì)失敗。
7.功能入口
項(xiàng)目發(fā)布到服務(wù)器后,用瀏覽器訪問項(xiàng)目中的index.jsp ,點(diǎn)擊下載鏈接,就可以彈出“下載”對(duì)話框。
- struts2實(shí)現(xiàn)簡單文件下載功能
- JSP開發(fā)之Struts2實(shí)現(xiàn)下載功能的實(shí)例
- struts2實(shí)現(xiàn)文件下載功能
- java中Struts2 的文件上傳和下載示例
- Struts2實(shí)現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)
- java中struts2實(shí)現(xiàn)簡單的文件上傳與下載
- java中struts2實(shí)現(xiàn)文件上傳下載功能
- JavaWeb中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- JavaEE中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
相關(guān)文章
Mybatis Plus整合PageHelper分頁的實(shí)現(xiàn)示例
這篇文章主要介紹了Mybatis Plus整合PageHelper分頁的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Java結(jié)構(gòu)性設(shè)計(jì)模式中的裝飾器模式介紹使用
裝飾器模式又名包裝(Wrapper)模式。裝飾器模式以對(duì)客戶端透明的方式拓展對(duì)象的功能,是繼承關(guān)系的一種替代方案,本篇文章以虹貓藍(lán)兔生動(dòng)形象的為你帶來詳細(xì)講解2022-09-09
淺談Java變量賦值運(yùn)算符及相關(guān)實(shí)例
這篇文章主要介紹了Java賦值運(yùn)算符的一些知識(shí),需要的朋友可以參考下。2017-09-09
Java中遇到的For?input?string問題解決辦法
這篇文章主要給大家介紹了關(guān)于Java中遇到的For?input?string問題的解決辦法,如果出現(xiàn)這樣的異常報(bào)錯(cuò),是指的數(shù)據(jù)轉(zhuǎn)換時(shí)出錯(cuò),文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
Spring Boot Actuator未授權(quán)訪問漏洞的問題解決
Spring Boot Actuator 端點(diǎn)的未授權(quán)訪問漏洞是一個(gè)安全性問題,可能會(huì)導(dǎo)致未經(jīng)授權(quán)的用戶訪問敏感的應(yīng)用程序信息,本文就來介紹一下解決方法,感興趣的可以了解一下2023-09-09
MyBatis中selectKey標(biāo)簽及主鍵回填實(shí)現(xiàn)
<selectKey>標(biāo)簽在MyBatis中提供了一種靈活的方式來生成和回填主鍵,本文就來介紹一下selectKey標(biāo)簽及主鍵回填實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12

