response下载csv文件内容乱码问题

时间:2021-12-29 08:02:33

response下载csv文件内容乱码问题

解决办法:在输出流语句第一行输出

out.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
ServletOutputStream out = response.getOutputStream();
out.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF});
out.write("电视机,20\n".getBytes("utf-8"));
out.write("洗衣机,10\n".getBytes("utf-8"));
out.write("冰箱,50\n".getBytes("utf-8"));

如果这中方式可以的话,那把字节流改成字符流应该也是可以的,但是事实却是不行,不知道为什么

//告诉浏览器用什么编码解析
response.setCharacterEncoding("UTF-8");
     PrintWriter out = response.getWriter();
out.write(new String(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF}));
out.write("电视机,20\n");
out.write("洗衣机,10\n");
out.write("冰箱, 10\n");