zoukankan      html  css  js  c++  java
  • SpringBoot发送文件到web端

    背景

    项目中要批量导入用户,但是导入的用户中信息可能有错误已注册注册成功等信息,需求是将这些用户的信息做区分,然后通过特定的文本形式,返回给web端。

    使用文本形式返回

    使用文本文件将所有的信息放入到指定文件中,然后通过流的形式传递到web端,代码如下:

    public void getText(HttpServletResponse response) throws IOException {
        log.info("请求接口返回文档");
        try {
            String info = "返回文档成功!";
            byte[] bytes = info.getBytes(StandardCharsets.UTF_8);
            response.setContentType("text/plain");
            response.setHeader("Content-Disposition", "attachment; filename="" + "hello.txt" + """);
            System.out.println("bytes.length = " + bytes.length);
            response.setContentLength(bytes.length);
            response.setHeader("Content-Range", "" + (bytes.length - 1));
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Etag", "W/"9767057-1323779115364"");
            OutputStream os = response.getOutputStream();
            os.write(bytes);
            //释放流
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    返回Excel文本内容

  • 相关阅读:
    Bellman算法
    Codeforces Round #378 (Div. 2) D
    运算符优先级
    Kruskal算法
    Java 大数运算
    无根树转有根树
    欧拉函数模板
    HDU 4135 Co-prime(容斥原理)
    快速求n的质因子(数论)
    Markdown中插入数学公式
  • 原文地址:https://www.cnblogs.com/hnxbp/p/15075045.html
Copyright © 2011-2022 走看看