1、下载的逻辑图
![整体流程图](https://i-blog.csdnimg.cn/blog_migrate/bcd131ac7d1b72233a6ac949089291f2.png)
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>