错误集

来源:互联网 发布:前段优化 编辑:程序博客网 时间:2024/05/16 09:19
中文作为url变量的时候在发起请求之前,变成uri是变为16进制字节码,后台在接受的时候要做一次字符转化
String namef=new String(name.getBytes("ISO-8859-1"),"UTF-8");
跟踪问题: 导入源码跟问题,或者直接网上查。
java.lang.StringIndexOutOfBoundsException: String index out of range: -213

由于要转的对象字段有乱码,在转成json时截断了json串

乱码解决方式:
在java后台装配时

String namef=new String(name.getBytes("ISO-8859-1"),"UTF-8");


@POST
    @Path("logs/EventId")
    @Produces(ContentType.APPLICATION_JSON_UTF_8)
    @Consumes(ContentType.APPLICATION_JSON_UTF_8)
    public long saveEventNameId(@Context HttpServletRequest request) throws ManoException
    {
        try
        {
        EventNameId operationLog = new EventNameId();
            operationLog.setEventId(request.getParameter("Event-Id"));
            String name =request.getParameter("Event-Name");
            String namef=new String(name.getBytes("ISO-8859-1"),"UTF-8");
            operationLog.setEventName(namef);
            operationLogLS.insertEventOne(operationLog);
            return operationLog.getId();
        }
        catch (Exception ex)
        {
            LOG.error(ex.getMessage(), ex);
            throw new ManoException(ex);
        }
    }

///这个偏前台url

当然也可以在url中加参数

  Client client = ClientBuilder.newClient();
            WebTarget target = client.target(url);
   //  target = target.queryParam("useUnicode", "true");
             //   target = target.queryParam("characterEncoding", "utf8");

这个是配合url的16进制的java代码转化,但是由于WebTarget 封装太强,即使转化成中文后面进行uri的时候也会被转回

String a= URLEncoder.encode("%E5%8B");
//            String output = URLDecoder.decode(a, "UTF-8");
//            System.out.println(URLDecoder.decode(output,"UTF-8"));

0 0
原创粉丝点击