前台页面
<h4> 动态文件下载</h4>
<a href="downloadServlet">download文件下载</a>
后台设计
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/x-msdownload");
String fileName = "文件下载.docx";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = response.getOutputStream();
String pptFileName = "F:\\自学材料\\sessioni笔记.docx";
InputStream in = new FileInputStream(pptFileName);
byte [] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1){
out.write(buffer, 0, len);
}
in.close();
}
前台也可以js控制
window.location.href='XXXXXXXXXX'
直接下载相应的文件,不用右键进行别存为
<h4> 动态文件下载</h4>
<a href="downloadServlet">download文件下载</a>
后台设计
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/x-msdownload");
String fileName = "文件下载.docx";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
OutputStream out = response.getOutputStream();
String pptFileName = "F:\\自学材料\\sessioni笔记.docx";
InputStream in = new FileInputStream(pptFileName);
byte [] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer)) != -1){
out.write(buffer, 0, len);
}
in.close();
}
前台也可以js控制
window.location.href='XXXXXXXXXX'
直接下载相应的文件,不用右键进行别存为