使用response.getOutputStream();下载文件内容为空

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    response.setContentType("application-msdown");
    String fileName = "xyz.txt";
    fileName = new String(fileName.getBytes("gbk"), "iso8859-1");
    response.setHeader("content-disposition", "attachment;filename=" + fileName);
    response.setCharacterEncoding("utf-8");

    OutputStream out = response.getOutputStream();
    String txtFileName = getServletContext().getRealPath(fileName);

    InputStream in = new FileInputStream(txtFileName);
    byte[] buffer = new byte[1024];
    int len = 0;

    while ((len = in.read()) > 0) {
        out.write(buffer, 0, len);
    }
    out.flush();
    in.close();
}

https://blog.csdn.net/huxy534/article/details/7599292