2019-07-30

U8D工具类

1.远程连接工具:

io.renren.common.utils.RemoteConnectedUtil:远程连接工具,传入第三方请求地址,调用第三方接口返回相应的值。

方法:
doGetJson(String url)

入参:
String类型字符串。为第三方的请求地址路径

返回值:

JSONObject对象。根据第三方的返回值将其返回值内容转为JSONObject对象,可以使用get(属性名)方式获取其具体的返回值内容

实现Code:

        JSONObject jsonObject=null;
        DefaultHttpClient client=new DefaultHttpClient();
        HttpGet httpGet=new HttpGet(url);
        HttpResponse response=client.execute(httpGet);
        HttpEntity entity=response.getEntity();
        if(entity!=null) {
            String result= EntityUtils.toString(entity,"utf-8");
            jsonObject=JSONObject.parseObject(result);
        }
        httpGet.releaseConnection();
        return jsonObject;

2.图片字符串互转工具

io.renren.common.utils.ImgBase64Utils:图片在base64字节码之间相互转化的工具类
方法:

  • base64ToImg:
    入参:
    HttpServletResponse 类型 response, 负责以图像方式输出base64编码图片
    String 类型 str ,图片的base64位字符串。
    返回值:
    一张图片
    实现代码:
        //设置输出样式为图片
        response.setContentType("image/*");
        if (str!=null){
            //删除前缀
            str=str.substring(22);
            BASE64Decoder base64Decoder=new BASE64Decoder();
            byte[] bytes=base64Decoder.decodeBuffer(str);
            for (int i=0;i<bytes.length;i++){
                //处理异常
                if (bytes[i]<0){
                    bytes[i]+=256;
                }
            }
            OutputStream outputStream=response.getOutputStream();
            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
        }
  • imgToBase64:
    入参:
    File类型 file,图片类型文件
    返回值:
    一串字符串

实现Code:

 byte[] data = null;
        try {
            InputStream in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        BASE64Encoder encoder = new BASE64Encoder();
        //添加前缀方便前台显示
        String result="data:image/png;base64,"+ encoder.encode(data);
        return result;
  • multipartFileToBase64:
    入参:
    MultipartFile类型 file :需要转换的图片
    String类型suffixRule:前缀限制,规则之间可以使用“;”分割
    double 类型 sizeRule :文件大小限制,单位是"M"

返回值:
一串base64编码的字符串。

实现逻辑:

         String img=null;
        if (file != null) {
            double size=file.getSize()/1024.00/1024.00;
            if (size>sizeRule&&sizeRule>0){
                throw new Exception(ConstantParam.SIZEERRORMESSAGE+sizeRule+"M");
            }
            String fileName=file.getOriginalFilename();
            boolean flag=false;
            if (fileName!=null&&suffixRule!=null){
                String suffix=fileName.substring(fileName.indexOf("."));
                flag=suffixRule.contains(suffix.toLowerCase());
            }
            if (!flag){
                throw new Exception(ConstantParam.SUFFIXERRORMESSAGE+suffixRule);
            }
            byte[] bytes = file.getBytes();
            BASE64Encoder encoder = new BASE64Encoder();
            img= "data:image/png;base64," + encoder.encode(bytes);
        }
        return img;

3.返回值处理工具

io.renren.common.utils.ResultHandleUtils:用于处理对象返回参数冗余的问题,可以打包需要的参数为map对象返回。
方法:

  • handle:
    入参:
    Object 对象object:需要进行结果处理的对象。
    String 类型对象若干个:其入参值要与对象的属性值相同才可以保证正确映射

返回值:一个map对象。

实现逻辑:采用class反射方式实现。

 Map<String,Object> result=new HashMap<>();
        Class<?> cls= object.getClass();
        for (String param:params){
            String methodName="get"+param.substring(0,1).toUpperCase()+param.substring(1);
            try {
                Method method=cls.getMethod(methodName);
                Object value=method.invoke(object);
                if ("null".equals(value)){
                    result.put(param,null);
                }else {
                    result.put(param,value);
                }

            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                result.put(param,"error,no such param");
            }
        }

        return result;

返回值例子:

处理前:

{companyId=null, companyCode=null, corpCode=null,......, updateTime=null}

处理后:此处只需要superAdminId,setupMan的字段,只传入String类型入参“superAdminId”,“setupMan”

{superAdminId=0, setupMan=0}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.使用mybitsPlus做插入操作时,出现ID特别大的情况 原因,mybitsPlus里默认自增ID是Long...
    割肉喂鹰丶阅读 406评论 0 0
  • day7-字典和集合 一.字典 1.什么是字典(dict) 容器型数据类型(序列),将{}作为容器的标志,里面多个...
    晨凡_2019阅读 191评论 0 0
  • review 1.列表: +, *, ==, !=, >, <, >=, <=, in/not inmax, mi...
    漫磋嗟阅读 189评论 0 0
  • 一、字典 1. 什么是字典(dict) 1)定义 字典是容器型数据类型(序列),将{}作为容器的标志,里面多个元素...
    Lis_reak阅读 121评论 0 0
  • Mybatis进阶总结 一·、#{}和${}的区别是什么? 1. #将传入的数据都当成一个字符串,会对自动传入的数...
    lipingLi_2019阅读 211评论 0 0