String jsonString = "{"姓名":"张三","年龄","18"}";
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
String fileName = URLEncoder.encode("UI", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".json");
OutputStream os = response.getOutputStream();
try {
byte[] bytes = jsonString.getBytes("UTF-8");
// 将字节流传入到响应流里,响应到浏览器
os.write(bytes);
} catch (Exception ex) {
throw new RuntimeException("导出失败");
}finally {
os.close();
}