日期:2014-05-19 浏览次数:20741 次
小弟最近在学习struts2文件下载 遇到个问题! 从前台传递一个文件名称:filename 路径我写死在程序中了! public String getFilename() { return filename; } public void setFilename(String filename) { try { String rString = new String(filename.getBytes("iso-8859-1"),"UTF-8"); this.filename=rString; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public InputStream getTargetFile()throws Exception { try { return ServletActionContext.getServletContext() .getResourceAsStream("\\download" + "\\" + filename); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return null; } struts.xml-------------------------------- <action name="download" class="com.hp.dl.action.DownloadFile"> <result name="tt" type="stream"> <param name="contentType">application/zip;charset=ISO8859-1</param> <!-- inputName默认值是inputStream,如果action中用于读取下载文件内容的属性名是inputStream,那么可以省略这个参数 --> <param name="inputName">targetFile</param> <!--动态获取文件名,从Action中的取得filename--> <param name="bufferSize">2048</param> <param name="contentDisposition">attachment;filename=${filename}</param> </result> </action>
<action name="download" class="com.hp.dl.action.DownloadFile"> <result name="tt" type="stream"> <param name="contentType">application/zip;charset=ISO8859-1</param> <!-- inputName默认值是inputStream,如果action中用于读取下载文件内容的属性名是inputStream,那么可以省略这个参数 --> <param name="inputName">targetFile</param> <!--动态获取文件名,从Action中的取得filename--> <param name="bufferSize">2048</param> <param name="contentDisposition">attachment;filename=${filename}</param> </result> </action>