struts2實現(xiàn)文件下載功能
更新時間:2018年03月30日 10:00:57 作者:平凡的華仔
這篇文章主要為大家詳細介紹了struts2實現(xiàn)文件下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了struts2下實現(xiàn)文件下載功能,供大家參考,具體內(nèi)容如下
下面以實現(xiàn)一個圖片下載功能為例:
1. 項目結(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 應用中的常量 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 配置上傳文件的最大容量,struts2默認為2M。單位是1B, 1KB=1024B,1M=1024KB,1M=1024*1024B--> <constant name="struts.multipart.maxSize" value="1048576" /> <!-- 配置本應用中的包,繼承 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元素所對應的value值downloadFile,在action中一定要有對應的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> 歡迎來到首頁,點下面鏈接去下載一個文件<br /> <a href="download.action?path=<%=" rel="external nofollow" ./download/a.jpg" %>">下載</a> </center> </body> </html>
6.文件路徑
項目中要提前建立好download目錄,和里面要存在有a.jpg文件,否則,下載會失敗。
7.功能入口
項目發(fā)布到服務(wù)器后,用瀏覽器訪問項目中的index.jsp ,點擊下載鏈接,就可以彈出“下載”對話框。
您可能感興趣的文章:
相關(guān)文章
使用Spring?Security搭建極簡的安全網(wǎng)站教程
這篇文章主要為大家介紹了使用Spring?Security搭建極簡的安全網(wǎng)站教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06