[问题解决方案]JSON parse error: Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using

公司最近跟外部有一个接受图片的接口让我来做,我心想这么简单 (又可以偷懒喽),打开文档一看图片是base64格式的,没啥,不就是base64转字节流吗。一同操作后 测试接受参数 崩出来这个东西。然后就解决呗 ,总不能因为解决不了让人转变发送参数的格式呗。
目前解决的博文都是倾向与修改参数 让参数中不要有转义字符,这怎么可以 ,base64要是动了,又要扯到字节流埙坏,图片一丢丢黑这咋行。。
所以就有了这一片文章喽。

首先分析为啥子会有这个异常
在这里插入图片描述

  • 嗯-.- 根据度娘的翻译我们知道 是因为json解析的问题 ,因为发送的字符中存在需要转移的字符,不然接受不到。
    这样啊 ,百分之80的问题来源就是发送的参数中带着转义字符 而后台用的是json接受 ,程序拿到
    **( [ { / ^ - $ ¦ } ] ) ? * + . ** 没有转义肯定不会解析了啊。

问题解决方案

	那就肯定有人要问楼主 你既然写博文是来解决问题的,我既然这样搜肯定会是需要输这些字符啊。
	不着急  楼主把自己的解决方案贴出来。
	其实思路很简单  
	 nested exception is com.fasterxml.jackson.databind.JsonMappingException  
	不是接受不了转义的参数吗。那我们拿request接受不就好了。
	我们不急 ,我把代码贴出来。

首先controller层接受参数的时候
要先去掉produces = "application/json;charset=UTF-8"

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


@RestController
@Slf4j
public class BiZiTeOtherController {
 @RequestMapping(value = "/test/UploadImage",method = RequestMethod.POST)
    public String uploadImageBiZiTe(HttpServletRequest request){
    	//GetRequestJsonUtils这个工具类在下面哦
		 String requestPostStr = GetRequestJsonUtils.getRequestPostStr(request);
        System.out.println(requestPostStr);
        //反正楼主用的能正常转成我需要的对象
        //<dependency>
          //  <groupId>com.alibaba</groupId>
            //<artifactId>fastjson</artifactId>
           // <version>1.2.47</version>
       // </dependency>
        UploadImg uploadImg = JSONObject.parseObject(requestPostStr, UploadImg.class);
        log.info(uploadImg.toString());
       return "成功";
    }
}

class  UploadImg{

    //图片分类
    private String picClass;
    //图片关联主键
    private String refId;
    //base64图片字符串
    private String pic;


    @Override
    public String toString() {
        return "uUploadImg{" +
                "picClass='" + picClass + '\'' +
                ", refId='" + refId + '\'' +
                ", pic='" + pic + '\'' +
                '}';
    }

    public String getPicClass() {
        return picClass;
    }

    public void setPicClass(String picClass) {
        this.picClass = picClass;
    }

    public String getRefId() {
        return refId;
    }

    public void setRefId(String refId) {
        this.refId = refId;
    }

    public String getPic() {
        return pic;
    }

    public void setPic(String pic) {
        this.pic = pic;
    }
}
/**
  *  转换reqeest工具类 
**/
public class GetRequestJsonUtils {

    public static JSONObject getRequestJsonObject(HttpServletRequest request) throws IOException {
        String json = getRequestJsonString(request);
        return JSONObject.parseObject(json);
    }
    /***
     * 获取 request 中 json 字符串的内容
     *
     * @param request
     * @return : <code>byte[]</code>
     * @throws IOException
     */
    public static String getRequestJsonString(HttpServletRequest request)
            throws IOException {
        String submitMehtod = request.getMethod();
        // GET
        if (submitMehtod.equals("GET")) {
            return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");
            // POST
        } else {
            return getRequestPostStr(request);
        }
    }
    /* @param request
     * @return
     * @throws IOException
     */
    public static byte[] getRequestPostBytes(HttpServletRequest request)
            throws IOException {
        int contentLength = request.getContentLength();
        if(contentLength<0){
            return null;
        }
        byte buffer[] = new byte[contentLength];
        for (int i = 0; i < contentLength;)
        {
            int readlen = request.getInputStream().read(buffer, i,
                    contentLength - i);
            if (readlen == -1) {
                break;
            }
            i += readlen;
        }
        return buffer;
    }
    /**
     * 描述:获取 post 请求内容
     * <pre>
     * 举例
     * </pre>
     * @param request
     * @return
     * @throws IOException
     */
    public static String getRequestPostStr(HttpServletRequest request) {
        try{
            byte buffer[] = getRequestPostBytes(request);
            String charEncoding = request.getCharacterEncoding();
            if (charEncoding == null) {
                charEncoding = "UTF-8";
            }
            return new String(buffer, charEncoding);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

据说点赞的都是帅哥。
不对,还有美女。



**参数中的这些转义字符不是必须接受的 可以通过前台限制用户输入这些字符,后台也就不用考虑到底  转不转   的过来不是。**
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值