50,526
社区成员
发帖
与我相关
我的任务
分享
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 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>