jsp下载服务器文件

1.java后端:保存待下载文件test.txt到realPath下面,其中:

String realPath = request.getSession().getServletContext().getRealPath("/");  

2.jsp端Java脚本:取项目访问路径basePath ,其中
<%
String contextPath = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort()+contextPath+"/";
%>
3.html标签中定义<input id="outputpath" type="hidden" value="<%=basePath %>"/>
4.js端:访问待下载资源window.location.href=document.getElementById("outputpath").value+"/test.txt";

猜你喜欢

转载自blog.csdn.net/qr457535344/article/details/79580647