jsp文件下载模板

download.jsp
<%@page import="java.io.*"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'download.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<jsp:include page="../res.jsp" />
</head>
<body>
	<%
    	/*
    		File 文件,文件夹
    		InputStream 输入流
    		OutputStream 输出流	
    	*/
    	InputStream is = null;
    	OutputStream os = null;
    	response.setContentType("application/octet-stream");
    	//attachment:附件
    	//inline:直接打开
    	response.setHeader("Content-Disposition","attachment;filename = test.txt");
    	try {
    		is = new FileInputStream(new File("D:/test.txt"));
    		os = response.getOutputStream();
    		byte[] bytes = new byte[1024];
    		int len = 0;
    		while((len = is.read(bytes)) != -1)
    			os.write(bytes,0,len);
    		os.flush();
    		//解决抛出异常
    		out.clear();
    		out = pageContext.pushBody();
     	} catch(Exception e) {
    		e.printStackTrace();
    	} finally {//无论发生什么,finally里的代码一定会执行
    		if(is != null) {
    			try {//如果is关闭失败,os必须也必须关闭掉
    				is.close();
    			} catch(Exception e) {
    				e.printStackTrace();
    			}
    		}
    		if(os != null) {
    			try {
    				os.close();
    			} catch(Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    %>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数学家是我理想

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值