关于生成csv文件并下载的问题

milamila26 2017-06-07 09:18:21
有一个需求,将查询到的list先导出成csv文件,然后再将这个csv文件从服务器下载到本地。我在网上找了个例子,按要求进行了配置,但是目前不报错,也没有弹出下载文件的窗口,请各位大侠帮帮忙看怎么修改。
下面上代码


//导出按钮,点击触发ajax事件
$("#export").click(function() {
var startDate = $("#start").val();
var endDate = $("#end").val();
$.ajax({
type : "POST",
url: "dailySettlementDownload.shtml?startDate="+startDate+"&endDate="+endDate,
dataType: "json",
success: function(data){
}
});
});



//controller层,接收请求参数
@RequestMapping(value="dailySettlementDownload")
@ResponseBody
public void dailySettlementDownload(Model model,HttpServletRequest request,HttpServletResponse response,HttpSession session,String startDateTime,String endDateTime){
String startDate = request.getParameter("startDate");
String endDate = request.getParameter("endDate");
TblMerBaseInfo merBaseInfo = (TblMerBaseInfo) UserSessionUtil.getUserFromSession(request);
merchantService2.dailySettlementDownload(merBaseInfo,startDate,endDate,request,response);
}



//service实现
@Override
public void dailySettlementDownload(TblMerBaseInfo merBaseInfo, String startDate, String endDate,
HttpServletRequest request, HttpServletResponse response) {
try {
CsmcTblMchntAcctRsltExample example = new CsmcTblMchntAcctRsltExample();
CsmcTblMchntAcctRsltExample.Criteria criteria = example.createCriteria();
if (merBaseInfo.getMerInnerCd() != null && !merBaseInfo.getMerInnerCd().equals("")) {
criteria.andMchntInnerCdEqualTo(merBaseInfo.getMerInnerCd());
}
if (startDate != null && !startDate.equals("") && endDate != null && !endDate.equals("")) {
startDate = startDate.substring(0, 10);
startDate = startDate.replace("/", "");
endDate = endDate.substring(0, 10);
endDate = endDate.replace("/", "");
criteria.andSettleDateBetween(startDate, endDate);
}
List<CsmcTblMchntAcctRslt> list = csmcTblMchntAcctRsltDao.selectByExample(example);

// 导出csv文件
LinkedHashMap map = new LinkedHashMap();
map.put("1", "清算日期");
map.put("2", "商户号");
map.put("3", "交易笔数");
map.put("4", "交易金额(分)");
map.put("5", "商户结算账户");
map.put("6", "商户结算账户名称");
map.put("7", "商户手续费结算账户");
map.put("8", "入账金额(分)");
map.put("9", "实际入账金额(分)");
map.put("10", "记账日期");
map.put("11", "记账流水号");

// excel在服务器中所在路径
String realPath = request.getRealPath("/WEB-INF/export"); // 获取绝对路径
String path = realPath.replace("\\", "/");
String fileName = "RiJie";
String fileds[] = new String[] { "settleDate", "mchntInnerCd", "transNum", "txnAmt", "mchntAcctAmtNo",
"mchntAcctAmtName", "mchntAcctFeeNo", "acctAmt", "actualMchntAcctAmt", "acctDate", "acctSsn" };
File file = CSVService3.createCSVFile(list, fileds, map, path, fileName);
String fileName2 = file.getName();
// 下载该文件到本地
this.download(path + "/" + fileName2, response);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 下载csv文件
*
* @param path
* @param response
*/
public void download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}


在路径E:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\MerPortal\WEB-INF\export生成了csv文件,但是没法下载,求教求教
...全文
505 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2017-06-08
  • 打赏
  • 举报
回复
不要用ajax,响应不了content-disposition这个头弹出保存对话框,直接location.href跳转下载,你的java增加get请求的配置 $("#export").click(function () { location.href="dailySettlementDownload.shtml?startDate=" + startDate + "&endDate=" + endDate; return; var startDate = $("#start").val(); var endDate = $("#end").val(); $.ajax({ type: "POST", url: "dailySettlementDownload.shtml?startDate=" + startDate + "&endDate=" + endDate, dataType: "json", success: function (data) { } }); });
milamila26 2017-06-08
  • 打赏
  • 举报
回复
引用 1 楼 showbo 的回复:
不要用ajax,响应不了content-disposition这个头弹出保存对话框,直接location.href跳转下载,你的java增加get请求的配置 $("#export").click(function () { location.href="dailySettlementDownload.shtml?startDate=" + startDate + "&endDate=" + endDate; return; var startDate = $("#start").val(); var endDate = $("#end").val(); $.ajax({ type: "POST", url: "dailySettlementDownload.shtml?startDate=" + startDate + "&endDate=" + endDate, dataType: "json", success: function (data) { } }); });
大神啊,收下我的膝盖吧

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧