struts2實(shí)現(xiàn)文件下載功能
文件下載是一個(gè)很常見(jiàn)的功能,用struts2實(shí)現(xiàn)文件下載的步驟:
一)、定義一個(gè)Action類,F(xiàn)ileDownload.java
package com.struts2.filedownload; import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; //文件下載 public class FileDownload extends ActionSupport{ private int number ; private String fileName; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } //返回一個(gè)輸入流,作為一個(gè)客戶端來(lái)說(shuō)是一個(gè)輸入流,但對(duì)于服務(wù)器端是一個(gè) 輸出流 public InputStream getDownloadFile() throws Exception { if(1 == number) { this.fileName = "Dream.jpg" ; //獲取資源路徑 return ServletActionContext.getServletContext().getResourceAsStream("upload/Dream.jpg") ; } else if(2 == number) { this.fileName = "jd2chm源碼生成chm格式文檔.rar" ; //解解亂碼 this.fileName = new String(this.fileName.getBytes("GBK"),"ISO-8859-1"); return ServletActionContext.getServletContext().getResourceAsStream("upload/jd2chm源碼生成chm格式文檔.rar") ; } else return null ; } @Override public String execute() throws Exception { return SUCCESS; } }
二)、在struts.xml文件中配置相關(guān)信息
<struts> <package name="struts2" extends="struts-default"> <action name="FileDownload" class="com.struts2.filedownload.FileDownload"> <result name="success" type="stream"> <param name="contentType">text/plain</param> <param name="contentDisposition">attachment;fileName="${fileName}"</param> <param name="inputName">downloadFile</param> <param name="bufferSize">1024</param> </result> </action> </package> </struts>
1.結(jié)果類型必須要寫成 type="stream" ,與之對(duì)應(yīng)的處理類是 org.apache.struts2.dispatcher.StreamResult
2.涉及到的參數(shù):
3.
1) <param name="contentDisposition">attachment;fileName="${fileName}"</param>
contentDisposition默認(rèn)是 inline(內(nèi)聯(lián)的), 比如說(shuō)下載的文件是文本類型的,就直接在網(wǎng)頁(yè)上打開,不能直接打開的才會(huì)打開下載框自己選擇
2) attachment :下載時(shí)會(huì)打開下載框
3) fileName="${fileName}" :在這定義的名字是一個(gè)動(dòng)態(tài)的,該名字是顯示在下載框上的文件名字
4. <param name="inputName">downloadFile</param> ,這個(gè)downloadFile名字要和FileDownload.java類中的getDownloadFile()方法名去掉get 一致
三)用于顯示下載的鏈接界面 filedownload.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>My JSP 'filedownload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <body> <h2>文件下載內(nèi)容:</h2><br/> Dream.jpg:<a href="FileDownload.action?number=1" rel="external nofollow" >點(diǎn)擊下載</a><br/> jd2chm源碼生成chm格式文檔.rar:<a href="FileDownload.action?number=2" rel="external nofollow" >點(diǎn)擊下載2</a> </body> </html>
文件上傳鏈接:
1)Servlet 文件上傳: 點(diǎn)擊打開鏈接
2)Struts2 文件上傳:點(diǎn)擊打開鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- struts2實(shí)現(xiàn)簡(jiǎn)單文件下載功能
- JSP開發(fā)之Struts2實(shí)現(xiàn)下載功能的實(shí)例
- 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í)例解析
- struts2實(shí)現(xiàn)文件下載功能
相關(guān)文章
詳解 Java Maximum redirects (100) exceeded
這篇文章主要介紹了詳解 Java Maximum redirects (100) exceeded的相關(guān)資料,需要的朋友可以參考下2017-05-05SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式
這篇文章主要介紹了SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Spring實(shí)戰(zhàn)之搜索Bean類操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之搜索Bean類操作,結(jié)合實(shí)例形式分析了Spring搜索Bean類的相關(guān)配置、接口實(shí)現(xiàn)與操作技巧,需要的朋友可以參考下2019-12-12SpringBoot整合WebSocket的客戶端和服務(wù)端的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot整合WebSocket的客戶端和服務(wù)端的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Java?導(dǎo)出Excel增加下拉框選項(xiàng)
這篇文章主要介紹了Java?導(dǎo)出Excel增加下拉框選項(xiàng),excel對(duì)于下拉框較多選項(xiàng)的,需要使用隱藏工作簿來(lái)解決,使用函數(shù)取值來(lái)做選項(xiàng),下文具體的操作詳情,需要的小伙伴可以參考一下2022-04-04Java的DataInputStream和DataOutputStream數(shù)據(jù)輸入輸出流
這里我們來(lái)看一下Java的DataInputStream和DataOutputStream數(shù)據(jù)輸入輸出流的使用示例,兩個(gè)類分別繼承于FilterInputStream和FilterOutputStream:2016-06-06Spring boot學(xué)習(xí)教程之快速入門篇
這篇文章主要給大家介紹了關(guān)于Spring boot的相關(guān)資料,本文屬于基礎(chǔ)入門教程,對(duì)各位學(xué)習(xí)Spring boot的新手們具有一定的參考學(xué)習(xí)價(jià)值,,要的朋友們下面來(lái)一起看看吧。2017-04-04java郵件發(fā)送簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java郵件發(fā)送簡(jiǎn)單實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03