我在action中拼装了字符串, 现在要将字符串以文件csv的方式导出,能够成功, 但是由于字符串过大, 耗内存, 下载很慢, 我该如何优化。 具体代码如下:
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "attachment; filename=" + new String("表名".getBytes("GB2312"), "ISO-8859-1") + ".csv");
response.setBufferSize(81920);
bos = new OutputStreamWriter(response.getOutputStream(), "GB2312");
bos.write(ordersInfo.toString());
bos.flush();
ps: ordersInfo 是我的字符串 请指点
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "attachment; filename=" + new String("表名".getBytes("GB2312"), "ISO-8859-1") + ".csv");
response.setBufferSize(81920);
bos = new OutputStreamWriter(response.getOutputStream(), "GB2312");
bos.write(ordersInfo.toString());
bos.flush();
ps: ordersInfo 是我的字符串 请指点
try {
bos = new OutputStreamWriter(response.getOutputStream(), "GB2312");
int cur = 0;
for(int i= 0;i < ordersInfo.toString().length();i += 1024){
bos.write(ordersInfo.toString(), i, 1024);
cur = 0;//记录当前写到的位置
bos.flush();
}
bos.write(ordersInfo.toString(), cur, ordersInfo.toString().length()-cur);//写出最后不足1024的部分
bos.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}