@ResponseBody
@RequestMapping("/export")
public byte[] exportPhone(HttpServletResponse response) throws UnsupportedEncodingException {
StringBuffer buff = new StringBuffer();
List<User> users = userService.findAll();
for (int i = 0; i < users.size(); i++) {
buff.append(users.get(i).toString());
buff.append("\r\n");
}
String fileName = "用户" + StringUtil.dateFormat("yyyy年MM月dd日HH时mm分ss秒", new Date()) + ".txt";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
return buff.toString().getBytes();
}
springboot 导出文件
最新推荐文章于 2025-11-05 18:41:14 发布
本文介绍了一个使用Java Spring框架实现的导出用户数据到TXT文件的方法。通过遍历所有用户信息,将数据转换为字符串并添加换行符,然后设置HTTP响应头以触发下载,最后返回包含所有用户信息的字节数组。
1965

被折叠的 条评论
为什么被折叠?



