页面代码:
<button id="btn2" type="button" class="btn btn-primary" onclick="DownLoad()">下载</button>
页面按钮js代码:
function DownLoad(){
window.open(url="http://localhost:8585/sm/sm/download");
}
spring后台下载功能代码:
需要设置请求头,请求体。
然后使用输入流读取文件,通过输出流发送文件。
@RequestMapping("/sm/download")
public void downLoad(String file, HttpServletRequest request,
HttpServletResponse response) {
String path = "F:\\MyEclipse 9Workspaces\\sm\\smfiledown\\";
File ofile = new File(path);
for (File p : ofile.listFiles()) {
if (!p.isDirectory()) {
String fname = p.getName();
FileInputStream fs = null;
if (p.exists()) {