public void kldTakeMobile() throws Exception{
    HttpServletResponse response = getResponse();
    List<String> strings = new ArrayList<>();
    strings.add("a");
    strings.add("b");
    strings.add("c");
    StringBuilder sb = new StringBuilder();
    for (int i=0;i<strings.size();i++){
        String value = strings.get(i);
        sb.append(value).append("\n");
    }
    //设置文件ContentType类型,这样设置,会自动判断下载文件类型
    response.setContentType("application/multipart/form-data");
    //设置文件头:最后一个参数是设置下载文件名
    response.setHeader("Content-Disposition", "attachment;filename="+ DateConvert.getTimeInt() +".txt");
    try{
        //用流将数据写给前端
        OutputStream os = response.getOutputStream();
        os.write(sb.toString().getBytes());
        os.flush();
        os.close();
    }catch (IOException ioe){
        ioe.printStackTrace();
    }
}原文:https://www.cnblogs.com/allJoy/p/14859524.html