Java导出格式化json文件
直接上代码:
private void downloadJsonFile(LifeDTO lifeDTO, HttpServletResponse response) {
String content = JSON.toJSONString(lifeDTO, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
BufferedOutputStream buff = null;
ServletOutputStream outputStream = null;
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.setHeader("Content-Disposition", "attachment;filename=data.json");
try {
outputStream = response.getOutputStream();
buff = new BufferedOutputStream(outputStream);
buff.write(content.getBytes(StandardCharsets.UTF_8));
buff.flush();
buff.close();
}catch (Exception e) {
log.error("downloadJsonFile error", e);
}finally {
try {
if (buff != null) {
buff.close();
}
if (outputStream != null) {
outputStream.close();
}
}catch (Exception e) {
log.error("io close error", e);
}
}
}
标签:outputStream,格式化,error,JSON,close,Java,null,response,buff
From: https://www.cnblogs.com/keyboardman/p/17555843.html