如何在jsp页面下载后台服务器返回的数据并保存为txt格式
2016-06-24 18:41
736 查看
首先新建一个jsp页面,jsp代码如下
<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 mypage">
<!--
<linkrel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#downloadbtn').click(function(){
alert("click");
var taskid="11";
//$.post("downLoad",{type:"download",TaskId:taskid});
var form=$("<form id='formmodify'action='downLoad' method='post'></form>");
form.append("<input type='hidden' name='taskid' value='"+taskid+"'>");
form.submit();
});
});
</script>
</head>
<body>
This is my JSP page. <br>
<input id="downloadbtn" type="button" value="download">
</body>
</html>
由jsp页面请求服务器后,将要请求的内容发送至服务器端的servlet(downLoad),服务器知道有浏览器请求后,将相应的内容写入txt文件的输出流,并由response返回客户端。
后台程序如下所示:
public void doPost(HttpServletRequestrequest, HttpServletResponseresponse)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
// 生成txt
try {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_kkmmss ");
String random = sdf.format(d);
String targetFile = random + ".txt";
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition",
"attachment; filename=\"" + targetFile + "\"");
OutputStream os = response.getOutputStream();
String temp = "你好啊! "+random;
System.out.println(temp);
os.write(temp.getBytes());
os.close();
response.flushBuffer();
} catch (Exceptione) {
System.out.println("生成txt文件时出错:");
e.printStackTrace();
}
}
打开页面点击如下所示download,请求穿回到后端服务器
然后,后端响应后,浏览器提供下载,默认就保存在了桌面
这个时候,桌面上就已经,出现了这个txt文件
打开txt,里面已经存在我保存进去的内容 “你好啊!。。”
<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 mypage">
<!--
<linkrel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="js/jquery-1.11.1.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#downloadbtn').click(function(){
alert("click");
var taskid="11";
//$.post("downLoad",{type:"download",TaskId:taskid});
var form=$("<form id='formmodify'action='downLoad' method='post'></form>");
form.append("<input type='hidden' name='taskid' value='"+taskid+"'>");
form.submit();
});
});
</script>
</head>
<body>
This is my JSP page. <br>
<input id="downloadbtn" type="button" value="download">
</body>
</html>
由jsp页面请求服务器后,将要请求的内容发送至服务器端的servlet(downLoad),服务器知道有浏览器请求后,将相应的内容写入txt文件的输出流,并由response返回客户端。
后台程序如下所示:
public void doPost(HttpServletRequestrequest, HttpServletResponseresponse)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
// 生成txt
try {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_kkmmss ");
String random = sdf.format(d);
String targetFile = random + ".txt";
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition",
"attachment; filename=\"" + targetFile + "\"");
OutputStream os = response.getOutputStream();
String temp = "你好啊! "+random;
System.out.println(temp);
os.write(temp.getBytes());
os.close();
response.flushBuffer();
} catch (Exceptione) {
System.out.println("生成txt文件时出错:");
e.printStackTrace();
}
}
打开页面点击如下所示download,请求穿回到后端服务器
然后,后端响应后,浏览器提供下载,默认就保存在了桌面
这个时候,桌面上就已经,出现了这个txt文件
打开txt,里面已经存在我保存进去的内容 “你好啊!。。”
相关文章推荐
- 小心服务器内存居高不下的元凶--WebAPI服务
- 运维入门
- 利用开源软件打造自己的全功能远程工具
- Linux5.9无人值守安装
- 数据中心和云未来的十二大趋势
- 用vsftp快速搭建ftp服务器
- Linux快速构建apache web服务器
- 服务器监控策略浅谈
- 如何降低服务器采购成本 原理分析
- 建议的服务器分区办法
- 服务器托管六大优势分析
- Erlang实现的一个Web服务器代码实例
- 服务器技术全面解析
- 保护DNS服务器的几点方法小结
- 我国成为全球第二大服务器消费国
- 服务器 安全检查要点[星外提供]
- 服务器应用自动重新启动IIS批处理[原创]_DOS/BAT_脚本之家
- FTP 服务器关于权限的问题
- 批处理设置windows服务器的代码ThecSafe1.9.4第1/3页