本文將介紹Struts2下載文件的方法實(shí)現(xiàn),包括對(duì)緩沖區(qū)的處理,下載文件的來源流處理等等。希望對(duì)大家操作Struts2下載文件有幫助。
Struts2下載文件實(shí)現(xiàn)的說明
contentType
內(nèi)容類型,和互聯(lián)網(wǎng)MIME標(biāo)準(zhǔn)中的規(guī)定類型一致,例如text/plain代表純文本,text/xml表示XML,image/gif代表GIF圖片,image/jpeg代表JPG圖片
inputName
下載文件的來源流,對(duì)應(yīng)著action類中某個(gè)類型為Inputstream的屬性名,例如取值為inputStream的屬性需要編寫getInputStream()方法
contentDisposition
文件下載的處理方式,包括內(nèi)聯(lián)(inline)和附件(attachment)兩種方式,而附件方式會(huì)彈出文件保存對(duì)話框,否則瀏覽器會(huì)嘗試直接顯示文件。取值為:
attachment;filename="struts2.txt",表示文件下載的時(shí)候保存的名字應(yīng)為struts2.txt。如果直接寫filename="struts2.txt",那么默認(rèn)情況是代表inline,瀏覽器會(huì)嘗試自動(dòng)打開它,等價(jià)于這樣的寫法:inline; filename="struts2.txt"
bufferSize
下載緩沖區(qū)的大小
Struts2下載實(shí)現(xiàn)源代碼
< ?xml version="1.0" encoding="UTF-8" ?> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "< A > http://struts.apache.org/dtds/struts-2.0.dtd< /A>"> < PACKAGE name="default" extends="struts-default"> < !-- 在這里添加Action定義 -->
< !-- 簡(jiǎn)單文件下載 --> < ACTION class=example.FileDownloadAction name="download"> < RESULT name="success" type="stream"> < PARAM name="contentType">text/plain< /PARAM> < PARAM name="inputName">inputStream< /PARAM> < PARAM name="contentDisposition">attachment;filename="struts2中文.txt"< /PARAM> < PARAM name="bufferSize">4096< /PARAM> < /RESULT> < /ACTION> < BR> < !-- 文件下載,支持中文附件名 --> < ACTION class=example.FileDownloadAction2 name="download2"> < !-- 初始文件名 --> < PARAM name="fileName">Struts中文附件.txt< /PARAM> < RESULT name="success" type="stream"> < PARAM name="contentType">text/plain< /PARAM> < PARAM name="inputName">inputStream< /PARAM> < !-- 使用經(jīng)過轉(zhuǎn)碼的文件名作為下載文件名,downloadFileName屬性 對(duì)應(yīng)action類中的方法 getDownloadFileName() --> < PARAM name="contentDisposition">attachment;filename="${downloadFileName}"< /PARAM> < PARAM name="bufferSize">4096< /PARAM> < /RESULT> < /ACTION> < !-- 下載現(xiàn)有文件 --> < ACTION class=example.FileDownloadAction3 name="download3"> < PARAM name="inputPath">/download/系統(tǒng)說明.doc< /PARAM> < !-- 初始文件名 --> < PARAM name="fileName">系統(tǒng)說明.doc< /PARAM> < RESULT name="success" type="stream"> < PARAM name="contentType">application/octet-stream;charset=ISO8859-1< /PARAM> < PARAM name="inputName">inputStream< /PARAM> < !-- 使用經(jīng)過轉(zhuǎn)碼的文件名作為下載文件名,downloadFileName屬性 對(duì)應(yīng)action類中的方法 getDownloadFileName() --> < PARAM name="contentDisposition">attachment;filename="${downloadFileName}"< /PARAM> < PARAM name="bufferSize">4096< /PARAM> < /RESULT> < /ACTION> < /PACKAGE>
|
【編輯推薦】
- 在Eclipse中開發(fā)struts應(yīng)用程序
- 手把手教你在Eclipse中配置開發(fā)Struts
- Eclipse下開發(fā)struts完整解決亂碼問題
- Struts相關(guān)背景介紹
- 使用Easy Struts for Eclipse開發(fā)Struts