HTML5的download属性
<a href="具体url(后端将文件以流形式返回)" rel="external nofollow" rel="external nofollow" rel="external nofollow" download="测试.xlsx(文件类型)" >下载</a>
效果如下图所示:
后端代码:
public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
OutputStream out = null;
try {
out = resp.getOutputStream();
int len = 0;
byte[] b = new byte[1024];
while ((len = inputStream.read(b)) != -1) {
out.write(b, 0, len);
}
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}