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過(guò)濾器 --> <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è)置歡迎頁(yè)面 --> <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í)打開(kāi)保存對(duì)話窗,filename表示下載時(shí)顯示的保存時(shí)的文件名 -->
<!-- 如果不寫(xiě)attachment;或者是寫(xiě)的是inline; 則表示內(nèi)聯(lián),即會(huì)在瀏覽器中嘗試打開(kāi)下載的文件,而不是下載-->
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>首頁(yè)</title> </head> <body> <center> 歡迎來(lái)到首頁(yè),點(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ù)器后,用瀏覽器訪問(wèn)項(xiàng)目中的index.jsp ,點(diǎn)擊下載鏈接,就可以彈出“下載”對(duì)話框。
- struts2實(shí)現(xiàn)簡(jiǎn)單文件下載功能
- JSP開(kāi)發(fā)之Struts2實(shí)現(xiàn)下載功能的實(shí)例
- struts2實(shí)現(xiàn)文件下載功能
- java中Struts2 的文件上傳和下載示例
- Struts2實(shí)現(xiàn)文件下載功能代碼分享(文件名中文轉(zhuǎn)碼)
- java中struts2實(shí)現(xiàn)簡(jiǎ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)文章
使用Spring?Security搭建極簡(jiǎn)的安全網(wǎng)站教程
這篇文章主要為大家介紹了使用Spring?Security搭建極簡(jiǎn)的安全網(wǎng)站教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
SpringBoot熱部署啟動(dòng)關(guān)閉流程詳解
Spring?Boot啟動(dòng)熱部署是一種技術(shù),它能讓開(kāi)發(fā)者在不重啟應(yīng)用程序的情況下實(shí)時(shí)更新代碼。這樣可以提高開(kāi)發(fā)效率,避免頻繁重啟應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-04-04
Java算法實(shí)戰(zhàn)之排一億個(gè)隨機(jī)數(shù)
我們?cè)谏钪薪?jīng)常遇見(jiàn)一些這樣的需求,隨機(jī)點(diǎn)名、公司年會(huì)抽獎(jiǎng)、微信拼手氣紅包等,還有一些游戲比如打地鼠小游戲、俄羅斯方塊等,這些場(chǎng)景中都會(huì)用到一種算法:隨機(jī),這篇文章主要給大家介紹了關(guān)于Java算法實(shí)戰(zhàn)之排一億個(gè)隨機(jī)數(shù)的相關(guān)資料,需要的朋友可以參考下2021-11-11
Mybatis插件之自動(dòng)生成不使用默認(rèn)的駝峰式操作
這篇文章主要介紹了Mybatis插件之自動(dòng)生成不使用默認(rèn)的駝峰式操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
springboot config 攔截器使用方法實(shí)例詳解
本文介紹Spring-Boot中使用攔截器的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05

