• Javaweb对接Unity(二)


    一、验证码

    这期要从Javaweb传一张图片到Unity,先上Java代码

    1. /**
    2. * 返回验证码图片
    3. */
    4. private void identity(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    5. //resp.setContentType("image/jpg");
    6. OutputStream os = resp.getOutputStream();
    7. //==把验证码放入Session
    8. //从请求中获取session
    9. HttpSession session = req.getSession();
    10. //生成图片输出到 响应的输出流 并返回生成的字符串
    11. // GifCaptcha gifCaptcha=new GifCaptcha();
    12. MathPngCaptcha captcha = new MathPngCaptcha(180, 80);
    13. String identityKey = captcha.out(os);
    14. //把数据放入session中 key,value
    15. // session.setAttribute("identityKey", identityKey);
    16. session.setAttribute(ProjectParam.SESSION_IDENTITY_KEY, identityKey);
    17. System.out.printf("验证码:%s\n", identityKey);
    18. //记得关闭流
    19. os.flush();
    20. os.close();
    21. }

    上面是一个返回验证码 图片的类

    然后再到C#代码,方法路径要对应设置TomCat的路径,上期讲过这里就不讲了

    1. private string identityUrl = "http://127.0.0.1:8080/PensionSystem/LoginServlet?method=identity&";
    2. private void Start() {
    3. StartCoroutine(ServletJsp(identityUrl));
    4. }
    5. IEnumerator ServletJsp(string path){
    6. DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
    7. request.downloadHandler = downloadTexture;
    8. yield return request.SendWebRequest();
    9. if (request.error != null)
    10. {
    11. Debug.Log(request.error);
    12. }
    13. else
    14. {
    15. identityImage.texture = downloadTexture.texture;
    16. Debug.Log(downloadTexture.texture);
    17. }
    18. }

    这里图片用的是RawImage,然后identityImage.texture = downloadTexture.texture接收

    在开始的时候用start()执行

    这样就可以得到验证码图片了 

    补充一个重要的点,需要在Java这边设置跨域,不然 session会出问题

    1. public class SimpleCORSFilter implements Filter {
    2. @Override
    3. public void init(FilterConfig filterConfig) throws ServletException {
    4. Filter.super.init(filterConfig);
    5. }
    6. @Override
    7. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    8. HttpServletResponse response = (HttpServletResponse) res;
    9. // 指定允许其他域名访问
    10. response.setHeader("Access-Control-Allow-Origin", "*"); // 允许所有
    11. // response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1, http://locahost"); // 允许白名单IP
    12. // 响应类型
    13. response.setHeader("Access-Control-Allow-Methods", "*");
    14. // 预检请求的结果缓存60分钟
    15. response.setHeader("Access-Control-Max-Age", "3600");
    16. //响应头设置
    17. response.setHeader("Access-Control-Allow-Headers", "*");
    18. response.setHeader("Access-Control-Allow-Credentials", "true");
    19. chain.doFilter(req, res);
    20. }
    21. @Override
    22. public void destroy() {
    23. Filter.super.destroy();
    24. }
    25. }

  • 相关阅读:
    yarn 环境突然用不了
    【我的OpenGL学习进阶之旅】OpenGL的坐标系的学习笔记
    兴达易控DP主站转TCP把ABB流量计接入到施耐德PLC
    【WMS仓库管理系统】基础概念:库位、库区、库位
    以太坊合并进展及合并后的 MEV 与矿工
    CAD进阶练习(六)、CAD创建块后图形依然保持原状?
    Golang 协程池 Ants 实现原理,附详细的图文说明和代码
    【docker:容器提交成镜像】
    HTML5期末大作业:基于HTML+CSS+JavaScript茶文化中国水墨风格绿色茶叶销售(5页) 学生网页设计作业源码
    GFS分布式文件系统
  • 原文地址:https://blog.csdn.net/a13828603242/article/details/127750665