request.getCookies()取不到自定义的cookie,而是内存的cookie

su_san_yun 2012-02-17 04:31:40
base64.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.net.URLEncoder" %>
<%@page import="sun.misc.BASE64Encoder;"%>
<%
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>Cookie Encoding</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>
<%
File file = new File (this.getServletContext().getRealPath("xishuangbanna.jpg"));
byte[] binary = new byte[(int)file.length()];
InputStream ins = this.getServletContext().getResourceAsStream("/" + file.getName());
ins.read(binary);
ins.close();

String content = BASE64Encoder.class.newInstance().encode(binary);
content.replace("\r\n", "<br>");
content.replace("\n", "<br>");

Cookie cookie = new Cookie("file", URLEncoder.encode(content));
response.addCookie(cookie);
%>
<body>
从Cookie中获取的二进制图片:<img src="base64_decode.jsp" /><br/>
<textarea id="cookieArea" style="width:100%;height:200px;"></textarea>
<script type="text/javascript">
cookieArea.value = document.cookie;
</script>
</body>
</html>

base64_decode.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="sun.misc.BASE64Decoder;"%>

<%
out.clear();
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equals("file")) {
byte[] binary = BASE64Decoder.class.newInstance().decodeBuffer(cookie.getValue().replace(" ", ""));

response.setHeader("Content-type", "image/jpg");
response.setHeader("Content-Disposition", "inline;filename=new.jpg");
response.setHeader("Connection", "close");
response.setContentLength(binary.length);
response.getOutputStream().write(binary);
response.getOutputStream().flush();
response.getOutputStream().close();
}
}
%>

base64.jsp和base64_decode.jsp都是在项目的根目录下
请高手指点
...全文
455 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
su_san_yun 2012-02-20
  • 打赏
  • 举报
回复
难道没有人知道吗?

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧