1、下载的逻辑图
![整体流程图](https://img-blog.csdnimg.cn/669cf277300e49a991f29e2630f08537.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5pif56m65LiL55qEMjEz,size_20,color_FFFFFF,t_70,g_se,x_16)
2、后端返回文件流
@Override
public void download(String name) {
String url = "地址"+name+".txt";
OutputStream out;
InputStream in;
File file = new File(url);
try {
in = new FileInputStream(file);
out = resp.getOutputStream();
int byteRead = 0;
byte[] buffer = new byte[512];
while ((byteRead = in.read(buffer)) != -1) {
out.write(buffer, 0, byteRead);
}
in.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3、前端vue接收转换
<script>
import axios from 'axios'
export default {
data() {
....
}
methods: {
download(row){
this.downloadData.url = row.url;
this.downloadData.name = row.name;
axios.defaults.baseURL = '指定url'
axios({
method: 'get',
url: "/name"+row.name,
responseType: 'arraybuffer',
})
.then(res => {
const data = res.data
const url = window.URL.createObjectURL(new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}))
const link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', '文件名称.txt')
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
})
}
}
}
</script>