struts2實(shí)現(xiàn)文件下載功能
本文實(shí)例為大家分享了struts2下實(shí)現(xiàn)文件下載功能,供大家參考,具體內(nèi)容如下
下面以實(shí)現(xiàn)一個圖片下載功能為例:
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超時定義,單位為分鐘 --> <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表示附件方式,即下載時打開保存對話窗,filename表示下載時顯示的保存時的文件名 --> <!-- 如果不寫attachment;或者是寫的是inline; 則表示內(nèi)聯(lián),即會在瀏覽器中嘗試打開下載的文件,而不是下載--> attachment;filename="a.jpg" </param> <!-- 下載文件輸出流定義 --> <!-- 這里的inputName元素所對應(yīng)的value值downloadFile,在action中一定要有對應(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)下面鏈接去下載一個文件<br /> <a href="download.action?path=<%=" rel="external nofollow" ./download/a.jpg" %>">下載</a> </center> </body> </html>
6.文件路徑
項(xiàng)目中要提前建立好download目錄,和里面要存在有a.jpg文件,否則,下載會失敗。
7.功能入口
項(xiàng)目發(fā)布到服務(wù)器后,用瀏覽器訪問項(xiàng)目中的index.jsp ,點(diǎn)擊下載鏈接,就可以彈出“下載”對話框。
- 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)文章
使用Spring?Security搭建極簡的安全網(wǎng)站教程
這篇文章主要為大家介紹了使用Spring?Security搭建極簡的安全網(wǎng)站教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Java算法實(shí)戰(zhàn)之排一億個隨機(jī)數(shù)
我們在生活中經(jīng)常遇見一些這樣的需求,隨機(jī)點(diǎn)名、公司年會抽獎、微信拼手氣紅包等,還有一些游戲比如打地鼠小游戲、俄羅斯方塊等,這些場景中都會用到一種算法:隨機(jī),這篇文章主要給大家介紹了關(guān)于Java算法實(shí)戰(zhàn)之排一億個隨機(jī)數(shù)的相關(guān)資料,需要的朋友可以參考下2021-11-11Mybatis插件之自動生成不使用默認(rèn)的駝峰式操作
這篇文章主要介紹了Mybatis插件之自動生成不使用默認(rèn)的駝峰式操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11springboot config 攔截器使用方法實(shí)例詳解
本文介紹Spring-Boot中使用攔截器的相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2018-05-05