struts2 文件下载
来源:互联网 发布:let it be 知乎 编辑:程序博客网 时间:2024/05/21 17:46
1.定义Action类(设定Web-inf/UploadImages下已经有3个图片)
package com.test.action;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownAction extends ActionSupport {
private String fileList[]=new String[]{
"1333546631718.jpg",
"张三.jpg",
"1333546632125.jpg"
};
private String DownDir="UploadImages/";
private String fileName;// 初始的通过param指定的文件名属性
public InputStream getInputStream() throws Exception {
String url=DownDir+getDownloadFileName();
System.out.println("url:"+url);
return ServletActionContext.getServletContext().getResourceAsStream(url);
}
/**
* 下载访问的Action
* @return
* @throws Exception
*/
public String DownAction() throws Exception {
return "downResult";
}
/**
* 显示文件列表访问的Action
* @return
* @throws Exception
*/
public String FileListAction() throws Exception {
return "fileListResult";
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
/** 提供转换编码后的供下载用的文件名 */
public String getDownloadFileName() {
String downFileName = fileName;
try {
downFileName = new String(downFileName.getBytes(), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}
public String[] getFileList() {
return fileList;
}
public void setFileList(String[] fileList) {
this.fileList = fileList;
}
public String getFileName() {
return fileName;
}
}
2.定义struts.xml
<action name="downAction" class="com.test.action.FileDownAction">
<result name="fileListResult">/FileUp/FileDown.jsp</result>
<result name="downResult" type="stream">
<!-- 指定只能下载pjpeg格式的图片 -->
<param name="contentType">image/pjpeg</param>
<!-- 对应Action中getInputStream()方法 -->
<param name="inputName">inputStream</param>
<!--动态获取文件名!-->
<param name="contentDisposition">attachment;filename="${DownloadFileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
3.定义Jsp
<table width="70%" border="1">
<caption>文件下载列表</caption>
<tr><td>文件名称</td><td>下载链接</td></tr>
<s:iterator value="fileList" status="sta">
<tr>
<td><s:property/></td>
<td>
<a href="<s:url value='downAction!DownAction.action'>
<s:param name='fileName'><s:property value='fileList[#sta.getIndex()]'/></s:param>
</s:url>
">下载</a>
</td>
</tr>
</s:iterator>
</table>
4.在浏览器地址中访问:http://localhost:8989/Struts2/downAction!FileListAction.action,点击下载即可
- struts2实现文件下载
- struts2文件下载
- Struts2控制文件下载
- struts2文件下载
- Struts2 文件下载
- ext + struts2 下载文件
- Struts2文件下载
- Struts2文件下载
- struts2文件下载
- Struts2文件下载
- Struts2 的文件下载
- Struts2 的文件下载
- Struts2文件的下载
- Struts2文件下载
- struts2.1文件下载
- struts2文件下载
- struts2下载文件
- struts2 文件下载
- Xib文件创建hello world
- struts2 文件上传(指定上传图片,单文件上传和批量上传放在一个jsp和Action中)
- QQ空间优化让百度收录你
- Qt + SQLite3 + VS 开发时出现乱码的解决方法
- [转】BIOS中断大全
- struts2 文件下载
- zoj 1828 Fibonacci Numbers
- PID控制算法举例之(一)
- Linux 简介
- struts2 Ognl用法(Ognl类的用法)
- 【合并正方形】石子合并
- struts2 Ognl关键概念
- heritrix3 伪装成GOOGLE进行爬取
- 11 csdn java 上课总结