数据生成txt文件,前端直接下载

public void getMotherCode (HttpServletRequest request, HttpServletResponse response,int number,String type) {
        //获取数据
        Set list = RandomNumberUtils.getRandomNumberList(number);
        //导出txt文件
        response.setContentType("text/plain");
        String fileName=type;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        response.setHeader("Content-Disposition","attachment; filename=" + fileName + ".txt");
        BufferedOutputStream buff = null;
        StringBuffer write = new StringBuffer();
        String enter = "\r\n";
        ServletOutputStream outSTr = null;
        try {
            // 建立
            outSTr = response.getOutputStream();
            buff = new BufferedOutputStream(outSTr);
            //把内容写入文件
            if(!list.isEmpty()){
                for (Object o : list) {
                    write.append(address);
                    write.append("/");
                    write.append(o);
                    write.append(enter);
                }
            }
            buff.write(write.toString().getBytes(StandardCharsets.UTF_8));
            buff.flush();
            buff.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                buff.close();
                outSTr.close();
            } catch (Exception e) {
                e.printStackTrace();

            }

        }
    }

你可能感兴趣的:(前端,java,postman)