关于CSV格式下载失败的问题

木木木_2016 2018-01-18 11:49:54
如图,生成CSV一切正常,就是下载失败,使用a标签下载也是失败,其他后缀的都没有问题
生成CSV文件代码

StringBuffer buf = new StringBuffer();

String[] colNamesArr = null;
String[] mapKeyArr = null;

colNamesArr = colNames.split(",");
mapKeyArr = mapKey.split(",");

// 完成数据csv文件的封装
// 输出列头
for (int i = 0; i < colNamesArr.length; i++) {
buf.append(colNamesArr[i]).append(CSV_COLUMN_SEPARATOR);
}
buf.append(CSV_RN);

if (null != dataList) { // 输出数据
for (int i = 0; i < dataList.size(); i++) {
for (int j = 0; j < mapKeyArr.length; j++) {
buf.append(dataList.get(i).get(mapKeyArr[j])).append(CSV_COLUMN_SEPARATOR);
}
buf.append(CSV_RN);
}
}
// 写出响应
os.write(buf.toString().getBytes("GBK"));
os.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;

下载代码

FileInputStream in = null;
OutputStream out = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// response.setContentType("text/csv;charset=utf-8");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(title, "utf-8"));
// response.setCharacterEncoding("utf-8");
//URLEncoder.encode(fileName, "GBK")
try {
in = new FileInputStream(path);
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (in != null) {
try {
in.close();
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

a标签下载

<a href="http://106.14.60.180:8090/MaJiangManage/statistics/aa.csv" download="w3logo">CSV下载失败</a>
<a href="http://106.14.60.180:8090/MaJiangManage/statistics/index2.jsp" download="w3logo">jsp下载成功</a>
...全文
1078 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
木木木_2016 2018-01-19
  • 打赏
  • 举报
回复
引用 1 楼 Royal_lr 的回复:
不是说没有找到该文件吗
文件存在
Royal_lr 2018-01-19
  • 打赏
  • 举报
回复
不是说没有找到该文件吗

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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