开发者社区> 问答> 正文

在action中使用response.getOutputStream() 设置cookie无效

public String yzm()throws Exception{
response.setCharacterEncoding("UTF-8");
Object[] obj = verification.RandomCount();//验证码 0 结果 1图片
//将验证结果放大session中,在登陆时验证
// session2.put("yzmResult", obj[0]);
OutputStream out = response.getOutputStream();
ImageIO.write((BufferedImage) obj[1], "JPEG", out);
//结果加到cookie
Cookie cookie = new Cookie("captcha", obj[0].toString());
cookie.setMaxAge(3600);//失效时间
cookie.setPath("/");
response.addCookie(cookie);
out.flush();
// out.close(); //系统会自动关闭它
return null;
}

我把 ImageIO.write((BufferedImage) obj[1], "JPEG", out); 注掉设置cookie没问题
不注掉 cookie 并没有被设置。怎么解决这个问题

展开
收起
蛮大人123 2016-05-26 18:34:29 2830 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    要在response.getOutputStream();之前设置 cookie 就没问题了.

    2019-07-17 19:16:47
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载