struts文件下载---自主写入下载内容
2013-11-30 21:52
330 查看
本文主要是为了说明如何在下载内容的时候自己写入内容。例如从list中获取内容然后写入下载文件中。
首先来分析下场景。查询出学生信息,需要将查询出来的信息进行下载,并进行保存(可自定义文件名和类型)。
下面下贴出struts.xml的代码:
然后是action的内容:
这里实现的主要原理就是,将list中的数据保存到字符串中,开始使用\n来换行,没有实现,后面经过度娘的帮助,使用\r\n解决了问题。
各系统应当是:
\r Mac
[b]\n Unix/Linux
\r\n Windows[/b]
现在解决了自定义下载内容的实现。
首先来分析下场景。查询出学生信息,需要将查询出来的信息进行下载,并进行保存(可自定义文件名和类型)。
下面下贴出struts.xml的代码:
<struts> <constant name="struts.i18n.encoding" value="gb2312"></constant> <package name="struts2" extends="struts-default" namespace="/upLoadFile"> <!-- 简单文件下载 --> <action name="download" class="com.nighthary.action.FileDownloadAction"> <result name="success" type="stream"> <param name="contentType">text/plain</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="downLoad.txt"</param> <param name="bufferSize">4096</param> </result> </action> </package> </struts>
然后是action的内容:
/** * struts实现的文件下载,用户可以自己写入内容以及保存的文件名 * @author NightHary * */ public class FileDownloadAction implements Action { public List<Student> studentList = new ArrayList<Student>(); public InputStream getInputStream() throws Exception { Map session = ActionContext.getContext().getSession(); studentList = (List<Student>)session.get("resultList"); String a = ""; for(int i=0;i<studentList.size();i++){ a += studentList.get(i).getId() +"\t"+studentList.get(i).getName() +"\t"+studentList.get(i).getAge()+"\r\n"; } return new ByteArrayInputStream(a.getBytes()); } public String execute() throws Exception { return SUCCESS; } }
这里实现的主要原理就是,将list中的数据保存到字符串中,开始使用\n来换行,没有实现,后面经过度娘的帮助,使用\r\n解决了问题。
各系统应当是:
\r Mac
[b]\n Unix/Linux
\r\n Windows[/b]
现在解决了自定义下载内容的实现。
相关文章推荐
- struts 让浏览器提示文件下载,而不是显示文件的内容
- struts-基础内容-8-文件下载
- FileSeek文件内容搜索工具下载
- Struts文件上传下载
- php对csv文件的读取,写入,输出下载操作
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- lua实现以字符串内容写入文件,成功返回 true,失败返回 false读取文件内容
- 把内容写入指定目录指定文件的java文件工具类,支持日期格式目录名的生成
- linux中echo命令向一个文件写入内容
- Struts2+jsp上传和下载文件
- java写入文件(文件内容末尾追加)
- ASP.NET创建文件并写入内容
- 第四讲 struts中的国际化与文件上传下载
- struts文件上传下载
- C# 文件操作(上传,下载,读取,写入)
- 有关web页面内容文件强制下载代码
- 以流的形式将文件写入页面,直接下载
- fwrite不能向文件中写入内容
- 如何把内容写入到文件
- 3.11文件下载和struts的注解及角色管理及dbutil的使用