java返回文件流

时间:2025-01-18 21:37:18
 /**
     * 返回文件流
     *
     * @param response
     */
    @PostMapping("/getFile")
    public void getFileInputStream(HttpServletResponse response) throws Exception {
//        File file = new File("/");
//        FileInputStream fis = new FileInputStream(file);
        InputStream is = ("test");
        ("application/x-msdownload;");
        ("Content-disposition", "attachment;filename=" + "文件名");
        //注:如果返回content-length的时候,先复制一下输入流,此时记录文件长度,返回复制之后的文件流
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(());
        byte[] buffer = new byte[2048];
        int len;
        while ((len = (buffer, 0, )) != -1) {
            (buffer, 0, len);
            ();
        }
        if (bis != null) {
            ();
        }
        if (bos != null) {
            ();
        }

    }

feign调用的时候,返回response,从response中获取返回的文件流,在返回出去

Response is = ();

InputStream inputStream =();