日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

struts2多文件动态下载及中文解决方案

發(fā)布時(shí)間:2023/12/15 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 struts2多文件动态下载及中文解决方案 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

作者:yan?

?

Action代碼:

package com.xxx.base.view.action; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import com.ruizcon.base.util.MyException;public class DownloadAction extends BaseAction {private String fileName;private String relativePath;public void setFileName(String fileName) throws UnsupportedEncodingException {/** 對(duì)fileName參數(shù)進(jìn)行UTF-8解碼,注意:實(shí)際進(jìn)行UTF-8解碼時(shí)會(huì)使用本地編碼。* 這里使用request.setCharacterEncoding解碼無(wú)效.* 只有解碼了getDownloadFile()方法才能在下載目錄下正確找到請(qǐng)求的文件* */String str=new String(fileName.getBytes("ISO8859-1"),"utf-8");this.fileName=str;}/*** @getFileName* 此方法對(duì)應(yīng)的是struts.xml文件中的:* <param >attachment;filename="${fileName}"</param>* 這個(gè)屬性設(shè)置的是下載工具下載文件時(shí)顯示的文件名,* 要想正確的顯示中文文件名,我們需要對(duì)fileName再次編碼* 否則中文名文件將出現(xiàn)亂碼,或無(wú)法下載的情況* @return* @throws UnsupportedEncodingException* @author XieYanZhou(Yan)* @date 2011-6-1* @version v1.0*/public String getFileName() throws UnsupportedEncodingException{String str= new String(fileName.getBytes(), "ISO8859-1");return str;}public void setRelativePath(String relativePath) throws UnsupportedEncodingException {//this.relativePath = relativePath;String str=new String(relativePath.getBytes("ISO8859-1"),"utf-8");this.relativePath=str;}/*** * @getDownloadFile* 此方法的命名是有規(guī)定的,對(duì)應(yīng)的是struts.xml文件中的:* <param >downloadFile</param>* 返回下載文件的流,可以參看struts2的源碼* @return* @author XieYanZhou(Yan)* @date 2011-6-1* @version v1.0*/public InputStream getDownloadFile(){/*String baseRealPath=this.getSession().getServletContext().getRealPath("");String path = baseRealPath + relativePath + "//" + this.fileName;path=path.replaceAll("/", "");FileInputStream fis = new FileInputStream(path);return fis;*/return this.getServletContext().getResourceAsStream(relativePath +"/"+ fileName);}@Overridepublic String execute() throws Exception {return SUCCESS;}} ?

?

xml配置:

<action name="downloadAction" class = "downloadAction"><result name="success" type="stream"><!-- <param name="contentType">application/excel,application/vnd.ms-excel</param> --><param name="contentDisposition">attachment;filename="${fileName}"</param><param name="inputName">downloadFile</param><param name="bufferSize">4096</param></result></action>?

?

頁(yè)面調(diào)用:

如:

<a href="base/downloadAction.do?relativePath=/userfiles/branchComInvWeekly&amp;fileName=濟(jì)南20110531184126875.xls">下載</a>

總結(jié)

以上是生活随笔為你收集整理的struts2多文件动态下载及中文解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。