假设你已经有一个byte[]数组,可以使用以下代码将其转换为文件下载链接:

String fileName = "example.txt";  // 文件名
byte[] fileBytes = // 从流中读取的字节数组
String contentType = "application/octet-stream";  // 文件类型

// 设置HTTP响应头
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

// 将字节数组写入响应输出流
OutputStream outputStream = response.getOutputStream();
outputStream.write(fileBytes);
outputStream.flush();
outputStream.close();

这段代码将字节数组写入HTTP响应输出流,并设置响应头使浏览器将其作为文件下载。在浏览器中访问该链接时,浏览器会自动下载文件

java中byte流的数据文件 如何转化为一个文件下载链接

原文地址: https://www.cveoy.top/t/topic/fHA6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录