response.setContentType("application/vnd.ms-excel;charset=UTF-8");
String fileName = "example.xlsx";
String contentDisposition = "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", contentDisposition);
response.setCharacterEncoding("UTF-8");
try (ServletOutputStream out = response.getOutputStream()) {
byte[] data = getYourData();
out.write(data);
out.flush();
} catch (IOException e) {
response.reset();
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().write("下载失败: " + e.getMessage());
}