mysql 已设置utf8mb4 还是存不了表情【解决方法】

1,emoji介绍

(1)emoji 就是表情符号,来自日语词汇“絵文字”(假名为“えもじ”,读音即 emoji)。
(2)最早由栗田穰崇(Shigetaka Kurita)创作,并在日本网络及手机用户中流行。 自苹果公司发布的 iOS 5 输入法中加入了 emoji 后,这种表情符号开始席卷全球。
(3)目前 emoji 已被大多数现代计算机系统所兼容的 Unicode 编码采纳,普遍应用于各种手机短信和社交网络中。
1535075603-7981-201610
2,问题描述

最近使用 MySQL 作为一个移动应用的数据库。但是不管使用 Anroid 设备,还是 iOS 设备。只要插入包含有 emoji 表情符号的记录时就报错。
1535075604-3223-201610

3,问题原因

MySQL 我使用的是默认的 utf8 编码,UTF8 编码只支持 1-3 个字节。而 emoji 占有 4 个字节的存储空间,所以自然保存不了。
从 MYSQL5.5 开始,可支持 4 个字节 UTF 编码,只要将编码标记成 utf8mb4 即可。并且 utf8mb4 是兼容 utf8 的。

csv文件上传中文编码乱码的解决方法

下面是上传文件的截图:

QQ截图20180814094435

1.文件是以GB2312作为编号,当上传至服务端后,编码依然是GB2312。所以一开始我尝试以GB2312编码进行转码,但是结果是失败的。

2.后来想到了写出文件的时候进行转码,成功把文件转成了UTF-8,但是会出现其他的bug,比如数字“1”会变成“1              ”。也就是多了很多空格,所以也失败了。

3.最后想到了,分割字符串,组装成对象那块,开始依然尝试使用GB2312、GBK、UTF-8依然失败,最后使用编码:ISO-8859-1 成功了。

下面是代码片段:

@Override
public Object readLineMap(String line) throws IllegalServiceException {
    if (line.contains(",")) {
        String xline = null;
        try {
            xline = new String(line.getBytes("ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        logger(xline);
        String[] strings = xline.split(",");


        ...........................

Linux系统可能需要这样子:

xline = new String(line.getBytes("ISO-8859-1"),"GB2312");

一款性能监控好工具 – NewRelic

在读《高性能MySql》一书中,认识了NewRelic,但是还未能及时用上,所以在网上找了一个帖子以记录。

NewRelic是一家提供Rails性能监测服务的网站, NewRelic提供了不同级别的监测功能,免费的是Lite版本,最高有Gold版本.

New Relic工具有两种运行模式:

(1)Production 模式:当您的工程以生产模式运行时,您可以在rpm.newrelic.com 网站上实时对它进行监督。

(2)Developer模式:当您的工程以开发模式运行时,您可以在本地localhost:3000/newrelic网页上查看到性能分析数据。

 

New Relic工具安装步骤(默认Rails 3版本):

  1. gem install newrelic_rpm
  2. 在Gemfile文件中,添加gem ‘newrelic_rpm’语句

3.在当前工程下,bundle update

  1. 在newrelic.com/get-RPM.html注册Lite版本的账号,然后将从此网站下载到的newrelic.yml文件,复制到当前工程的config文件夹中。
  2. 在development模式下启动工程,访问工程,然后在http://localhost:3000/newrelic页面您会看到对当前工程的性能分析数据。

6.若在production模式下启动工程,则您访问http://localhost:3000/newrelic链接会失效,这时您可以通过https://rpm.newrelic.com/login进行登录,在NewRelic的网站上查看关于工程的性能分析数据。

基于 Swagger 的前后端分离开发实践

前后端分离开发已经是很流行的一个开发模式。前端开发不需要部署后端语言的环境,后端开发也不需要前端写好的任何程序。后端只管暴露各种 API 接口供给前端进行数据的增、删、改、查,不负责生成 HTML 页面,这种方式能够减轻后端任务让后端开发更加专注。尤其是在微服务的开发框架下, 前后端分离开发的模式应用更加广泛。本篇亦是在微服务的开发框架下的实践总结。

在微服务开发框架下,前端通常被设计成一个独立的微服务。前后端仅仅通过接口来协作,前端服务最终生成一个独立的 Docker 镜像来部署。在产品的核心微服务定义完成后,我们希望前后端 Service 同时开始开发,所以这里我们利用 Swagger 创建了一个基于 Node.js 的 Mock Server 作为前后端分离开发的工具。在后端服务没有完全实现的情况下, 使用 Mock Server 作为前端开发的支持工具, 来实现前后端同时开发的目的。

Java源码ImageVerifyUtils验证码工具类

应用于验证码校验与生成,一个简单的验证码生成类。

功能结合了,生成验证码,及验证码校验,需要传入http session。

直接上源码:

package com.mymvc.system.utils;

import com.mymvc.system.exception.IllegalValidateException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

/**
 * image verify class.
 * <p>
 * ImageVerifyUtils verify = new ImageVerifyUtils();
 * verify.make(response, request.getSession());
 * <p>
 * verify.check(request.getSession(),code); => true or throw new Exception.
 */
public class ImageVerifyUtils {

    private static final Logger log = Logger.getLogger(ImageVerifyUtils.class.getName());

    private String KEY_SESSION_VERIFY = "key_session_image_verify";
    private String KEY_SESSION_DATETIME = "key_session_time_verify";

    private Long expireIn = 300000L;
    private String hash = "hash";

    //    private String raw = "qwertyupkijhgfdsazxcvbnmABCDEFGHJKLMNPQRSTUVWXYZ1234567890";
    private String raw = "1234567890";

    public ImageVerifyUtils() {

    }

    public void make(HttpServletResponse response, HttpSession session) throws Exception {
        this.make(response, session, 120, 40);
    }

    public void make(HttpServletResponse response, HttpSession session, int width, int height) throws Exception {
        this.make(response, session, width, height, 4);
    }

    /**
     * make a image verify
     * @param response output to browser.
     * @param session http session,use it to save verify code.
     * @param width default is 120px
     * @param height default is 40px
     * @param num the verify code.
     * @throws Exception
     */
    public void make(HttpServletResponse response, HttpSession session, int width, int height, int num) throws Exception {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        int r = this.getRandom(200, 255);
        int g = this.getRandom(200, 255);
        int b = this.getRandom(200, 255);

        Graphics2D g2d = (Graphics2D) image.getGraphics();
        g2d.setColor(new Color(r, g, b));
        g2d.fillRect(0, 0, width, height);

        g2d.setStroke(new BasicStroke(1.5f));
        for (int i = 0; i < 5; i++) {
            r = this.getRandom(50, 200);
            g = this.getRandom(50, 200);
            b = this.getRandom(50, 200);
            g2d.setColor(new Color(r, g, b));

            g2d.drawLine(getRandom(0, width), getRandom(0, height), getRandom(0, width), getRandom(0, height));
        }

        int fontSize = (width - 26) / num;

        r = this.getRandom(0, 180);
        g = this.getRandom(0, 180);
        b = this.getRandom(0, 180);
        g2d.setColor(new Color(r, g, b));
        g2d.setFont(new Font(null, Font.PLAIN, fontSize));
        List<String> codes = this.getVerifyString(num);
        String code = "";

        int __x = (width - fontSize * num) / 2;
        for (int i = 0; i < codes.size(); i++) {
            code += codes.get(i);
            if (i == 0) {
                g2d.drawString(codes.get(i), __x, getRandom(20, height));
            } else {
                g2d.drawString(codes.get(i), fontSize * i + __x, getRandom(20, height));
            }
        }

        log.info(">>code:" + code);
        //绘制干扰点
        g2d.setStroke(new BasicStroke(2.6f));
        for (int i = 0; i < 10; i++) {
            r = this.getRandom(100, 255);
            g = this.getRandom(100, 255);
            b = this.getRandom(100, 255);
            g2d.setColor(new Color(r, g, b));

            int x = getRandom(0, width);
            int y = getRandom(0, height);
            g2d.drawLine(x, y, x + 2, y + 2);
        }

        session.setAttribute(KEY_SESSION_VERIFY, Md5Utils.md5(code.toLowerCase() + hash).toUpperCase());
        session.setAttribute(KEY_SESSION_DATETIME, System.currentTimeMillis() + expireIn);

        response.setContentType("image/jpeg");
        OutputStream ops = response.getOutputStream();
        ImageIO.write(image, "jpeg", ops);
        ops.close();
    }

    /**
     * check the code valid or invalid.
     * @param session Http session
     * @param verifyCode code string
     * @return true or throws.
     * @throws IllegalValidateException
     */
    public boolean check(HttpSession session, String verifyCode) throws IllegalValidateException {

        if (verifyCode == null) {
            throw new IllegalValidateException("请输入验证码");
        }

        Long makeTime = (Long) session.getAttribute(KEY_SESSION_DATETIME);
        if (makeTime == null) {
            throw new IllegalValidateException("验证码不存在");
        }

        if (makeTime.longValue() < System.currentTimeMillis()) {
            throw new IllegalValidateException("验证码已过期");
        }

        String source = (String) session.getAttribute(KEY_SESSION_VERIFY);
        if (source == null) {
            throw new IllegalValidateException("验证码不存在");
        }

        String now = Md5Utils.md5(verifyCode.toLowerCase() + hash).toUpperCase();
        if (now.equals(source)) {
            session.removeAttribute(KEY_SESSION_DATETIME);
            session.removeAttribute(KEY_SESSION_VERIFY);
            return true;
        }

        throw new IllegalValidateException("验证码错误");

    }

    /**
     * get verify code.
     * @param num the code length,default is 4.
     * @return
     */
    private List<String> getVerifyString(int num) {
        if (num <= 0) {
            num = 4;
        }
        List<String> res = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < num; i++) {
            res.add(String.valueOf(raw.charAt(r.nextInt(raw.length()))));
        }
        return res;
    }

    /**
     * get number by random.
     * @param min min number
     * @param max max number
     * @return between min to max.
     */
    private int getRandom(int min, int max) {
        Random random = new Random();
        return random.nextInt(max) % (max - min + 1) + min;
    }

}

 

使用方法(生成):

ImageVerifyUtils verify = new ImageVerifyUtils();
try {
    verify.make(response, request.getSession());
} catch (Exception e) {
    e.printStackTrace();
}

校验:

ImageVerifyUtils verify = new ImageVerifyUtils();
try {

    String code = request.getParameter("code");
    verify.check(request.getSession(),code);
} catch (IllegalValidateException e) {
    e.printStackTrace();
}

 

 

1232425262732
 
Copyright © 2008-2021 lanxinbase.com Rights Reserved. | 粤ICP备14086738号-3 |