public void downloadCsv(List<RepairCatalogSearchResultsVO> rowDataList) throws CROSException{
try{
/* File file = new File("D:csv/result/results.csv");
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(out);
BufferedWriter bw = new BufferedWriter(osw);*/
ServletOutputStream servletOutputStream = null;
FileInputStream input = null;
File file = null;
FileOutputStream out = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
//insert data
String filePath = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRealPath("/")
+ "WEB-INF\\" + System.currentTimeMillis() + "_" + "results.csv";
out = new FileOutputStream(filePath);
osw = new OutputStreamWriter(out);
bw = new BufferedWriter(osw);
bw.write("ATA");
bw.write(",");
bw.write("componentDesc");
bw.write(" ") ;
bw.newLine();
Iterator<RepairCatalogSearchResultsVO> ite = rowDataList.iterator();
while (ite.hasNext()) {
RepairCatalogSearchResultsVO vo = ite.next();
bw.write(vo.getAtaNum());
bw.write(",");
bw.write(vo.getComponentDesc());
bw.write(" ") ;
bw.newLine();
}
//close
out.flush();
out.close();
//以下代码是用来下载文件时,由用户选择保存的地址。
file = new File(filePath);
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
servletOutputStream = httpServletResponse.getOutputStream();
httpServletResponse.setHeader("Content-disposition",
"attachment; filename=" + "results.csv");
httpServletResponse.setContentLength((int) file.length());
httpServletResponse.setContentType("application/x-download");
httpServletResponse.setContentType("application/csv");
byte[] buffer = new byte[1024];
int flag = 0;
input = new FileInputStream(file);
while ((flag = input.read(buffer)) > 0) {
servletOutputStream.write(buffer, 0, flag);
}
servletOutputStream.flush();
bw.close();
osw.close();
out.close();
}
catch(Exception e){
e.printStackTrace() ;
}
} 详细解决方案
为什么小弟我这段生成csv文件的代码,上载好文件之后,输出的是jsp对应的html页面呢
热度:5545 发布时间:2013-02-25 21:40:09.0
相关解决方案
- JSP 页面乱码 页面起首已设置 contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
- html js不运行有关问题
- <html:text>property的有关问题
- 关于<html:text>相关的有关问题
- 简单计算器(html+js),该怎么解决
- HTML 小疑点,哪位高手解决 给哪位高手分
- 嵌入JSP中的CSS显示效果和在 HTML 中的不一样.为什么.该怎么解决
- struts1 中<html:checkbox>标签的value值怎么动态赋值
- 新人求问,J2EE方向,html,css,javascript,vml要学到什么程度?解决思路
- :前台和后台开发有啥区别?还有css、html、ajax、js、jquery都有什么区别
- struts <html:file> 怎么让前面的框消失,先谢过了
- =Html.TextAreaFor的文本区域大小如何设置
- 正则表达式 提取 html 标签的内容,该如何解决
- @Html.CkEditor,该怎么处理
- string msg = (string)html.ViewData["Message"];该如何处理
- html password editbox 编码有关问题
- @Html.TextBoxFor(a => a.Title 上边有红线,说异常
- 关于伪静态,怎么配置.html
- MVC中,Html.DropDownListFor怎么绑定数据
- 为何FCKeditor会在自动生成<html>标签呢
- MVC 中得 Html.ActionLink 怎么linkText显示图片标记
- HTML DOM setTimeout() 跟 setInterval() 的区别 ?———急——
- 怎么自动执行<a href="document.html" target="parent">my document</a> 呢
- MVC2 怎么实现伪静态 .html
- MVC中@Html.TextBox怎么使用
- CSDN 你是怎么做到的呢 html?xxx
- 问一个关于去除<html>标签的有关问题
- webserver的“text/html;charset=utf-8”,但应为“text/xml”异常
- mvc @using (Html.BeginForm()),该怎么处理
- @Html.TextBoxfor的有关问题