文件下载(语音,文件)

一,注意path地址是http请求访问到的地址,而不是文件路径

//后台代码

response.setHeader("Content-Disposition", "attachment;filename="+filename);

URL url = new URL(path);
// path是指欲下载的文件的路径。
URLConnection conn = url.openConnection();
InputStream fis = conn.getInputStream();
OutputStream out = response.getOutputStream();
//写文件
int b;
while((b=fis.read())!= -1){
out.write(b);
}
fis.close();
out.close();

这是浏览器直接下载

第二种,前端js方式

window.open('https://1.1.1.1/test.txt')

原文地址:https://www.cnblogs.com/yangyang2018/p/10237574.html