java将文件流返回给前端

时间:2025-01-18 21:37:01

需求: 开始 --> 读取文件 --> 将文件流返回给前端 --> 结束

1、读取文件

// 读取文件
File file = new File("path/to/your/"); // 文件路径
FileInputStream fis = new FileInputStream(file);

2、将文件流返回给前端 

// 将文件流返回给前端
("application/octet-stream");
("Content-Disposition", "attachment; filename=\"" + () + "\"");

OutputStream outputStreams = ();
byte[] buffer = new byte[1024];
int len;
while ((len = (buffer)) != -1) {
    (buffer, 0, len);
}
outputStreams .flush();

();
();