APP表情java后台处理
2018-01-05 00:43
162 查看
今天项目遇到的一个问题,APP订单评论提交emoji表情的话会报错,由于需要实现emoji表情评论的功能,根据自己实现的方式以及查询的实现方式,现总结为一下几种
mysql的utf8编码的一个字符最多3个字节,但是一个emoji表情为4个字节,所以utf8不支持存储emoji表情。
具体方法可参考连接
这种方式后台不用再做额外的操作,直接正常存储即可,但是修改数据库会比较麻烦
参考这篇文章,这里不再叙述
mysql的utf8编码的一个字符最多3个字节,但是一个emoji表情为4个字节,所以utf8不支持存储emoji表情。
实现方式一
对APP传入后端的数据包括字符串和emoji表情,把emoji表情和String都进行Base64转换,存储在数据库中,取的时候再转换回来即可解决上述问题,这是最快也是最简单的解决方法。import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; /** * <p> * Base64 encoding and decoding utility methods, both for binary and textual * informations. * </p> * * @author Carlo Pelliccia * @since 1.1 * @version 1.3 */ public class Base64 { /** * <p> * Encodes a string. * </p> * <p> * Before the string is encoded in Base64, it is converted in a binary * sequence using the system default charset. * </p> * * @param str * The source string. * @return The encoded string. * @throws RuntimeException * If an unexpected error occurs. */ public static String encode(String str) throws RuntimeException { byte[] bytes = str.getBytes(); byte[] encoded = encode(bytes); try { return new String(encoded, "ASCII"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("ASCII is not supported!", e); } } /** * <p> * Encodes a string. * </p> * <p> * Before the string is encoded in Base64, it is converted in a binary * sequence using the supplied charset. * </p> * * @param str * The source string * @param charset * The charset name. * @return The encoded string. * @throws RuntimeException * If an unexpected error occurs. * @since 1.2 */ public static String encode(String str, String charset) throws RuntimeException { byte[] bytes; try { bytes = str.getBytes(charset); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unsupported charset: " + charset, e); } byte[] encoded = encode(bytes); try { return new String(encoded, "ASCII"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("ASCII is not supported!", e); } } /** * <p> * Decodes the supplied string. * </p> * <p> * The supplied string is decoded into a binary sequence, and then the * sequence is encoded with the system default charset and returned. * </p> * * @param str * The encoded string. * @return The decoded string. * @throws RuntimeException * If an unexpected error occurs. */ public static String decode(String str) throws RuntimeException { byte[] bytes; try { bytes = str.getBytes("ASCII"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("ASCII is not supported!", e); } byte[] decoded = decode(bytes); return new String(decoded); } /** * <p> * Decodes the supplied string. * </p> * <p> * The supplied string is decoded into a binary sequence, and then the * sequence is encoded with the supplied charset and returned. * </p> * * @param str * The encoded string. * @param charset * The charset name. * @return The decoded string. * @throws RuntimeException * If an unexpected error occurs. * @since 1.2 */ public static String decode(String str, String charset) throws RuntimeException { byte[] bytes; try { bytes = str.getBytes("ASCII"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("ASCII is not supported!", e); } byte[] decoded = decode(bytes); try { return new String(decoded, charset); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Unsupported charset: " + cha 4000 rset, e); } } /** * <p> * Encodes a binary sequence. * </p> * <p> * If data are large, i.e. if you are working with large binary files, * consider to use a {@link Base64OutputStream} instead of loading too much * data in memory. * </p> * * @param bytes * The source sequence. * @return The encoded sequence. * @throws RuntimeException * If an unexpected error occurs. * @since 1.2 */ public static byte[] encode(byte[] bytes) throws RuntimeException { return encode(bytes, 0); } /** * <p> * Encodes a binary sequence, wrapping every encoded line every * <em>wrapAt</em> characters. A <em>wrapAt</em> value less than 1 disables * wrapping. * </p> * <p> * If data are large, i.e. if you are working with large binary files, * consider to use a {@link Base64OutputStream} instead of loading too much * data in memory. * </p> * * @param bytes * The source sequence. * @param wrapAt * The max line length for encoded data. If less than 1 no wrap * is applied. * @return The encoded sequence. * @throws RuntimeException * If an unexpected error occurs. * @since 1.2 */ public static byte[] encode(byte[] bytes, int wrapAt) throws RuntimeException { ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { encode(inputStream, outputStream, wrapAt); } catch (IOException e) { throw new RuntimeException("Unexpected I/O error", e); } finally { try { inputStream.close(); } catch (Throwable t) { ; } try { outputStream.close(); } catch (Throwable t) { ; } } return outputStream.toByteArray(); } /** * <p> * Decodes a binary sequence. * </p> * <p> * If data are large, i.e. if you are working with large binary files, * consider to use a {@link Base64InputStream} instead of loading too much * data in memory. * </p> * * @param bytes * The encoded sequence. * @return The decoded sequence. * @throws RuntimeException * If an unexpected error occurs. * @since 1.2 */ public static byte[] decode(byte[] bytes) throws RuntimeException { ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { decode(inputStream, outputStream); } catch (IOException e) { throw new RuntimeException("Unexpected I/O error", e); } finally { try { inputStream.close(); } catch (Throwable t) { ; } try { outputStream.close(); } catch (Throwable t) { ; } } return outputStream.toByteArray(); } /** * <p> * Encodes data from the given input stream and writes them in the given * output stream. * </p> * <p> * The supplied input stream is read until its end is reached, but it's not * closed by this method. * </p> * <p> * The supplied output stream is nor flushed neither closed by this method. * </p> * * @param inputStream * The input stream. * @param outputStream * The output stream. * @throws IOException * If an I/O error occurs. */ public static void encode(InputStream inputStream, OutputStream outputStream) throws IOException { encode(inputStream, outputStream, 0); } /** * <p> * Encodes data from the given input stream and writes them in the given * output stream, wrapping every encoded line every <em>wrapAt</em> * characters. A <em>wrapAt</em> value less than 1 disables wrapping. * </p> * <p> * The supplied input stream is read until its end is reached, but it's not * closed by this method. * </p> * <p> * The supplied output stream is nor flushed neither closed by this method. * </p> * * @param inputStream * The input stream from which clear data are read. * @param outputStream * The output stream in which encoded data are written. * @param wrapAt * The max line length for encoded data. If less than 1 no wrap * is applied. * @throws IOException * If an I/O error occurs. */ public static void encode(InputStream inputStream, OutputStream outputStream, int wrapAt) throws IOException { Base64OutputStream aux = new Base64OutputStream(outputStream, wrapAt); copy(inputStream, aux); aux.commit(); } /** * <p> * Decodes data from the given input stream and writes them in the given * output stream. * </p> * <p> * The supplied input stream is read until its end is reached, but it's not * closed by this method. * </p> * <p> * The supplied output stream is nor flushed neither closed by this method. * </p> * * @param inputStream * The input stream from which encoded data are read. * @param outputStream * The output stream in which decoded data are written. * @throws IOException * If an I/O error occurs. */ public static void decode(InputStream inputStream, OutputStream outputStream) throws IOException { copy(new Base64InputStream(inputStream), outputStream); } /** * <p> * Encodes data from the given source file contents and writes them in the * given target file, wrapping every encoded line every <em>wrapAt</em> * characters. A <em>wrapAt</em> value less than 1 disables wrapping. * </p> * * @param source * The source file, from which decoded data are read. * @param target * The target file, in which encoded data are written. * @param wrapAt * The max line length for encoded data. If less than 1 no wrap * is applied. * @throws IOException * If an I/O error occurs. * @since 1.3 */ public static void encode(File source, File target, int wrapAt) throws IOException { InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = new FileInputStream(source); outputStream = new FileOutputStream(target); Base64.encode(inputStream, outputStream, wrapAt); } finally { if (outputStream != null) { try { outputStream.close(); } catch (Throwable t) { ; } } if (inputStream != null) { try { inputStream.close(); } catch (Throwable t) { ; } } } } /** * <p> * Encodes data from the given source file contents and writes them in the * given target file. * </p> * * @param source * The source file, from which decoded data are read. * @param target * The target file, in which encoded data are written. * @throws IOException * If an I/O error occurs. * @since 1.3 */ public static void encode(File source, File target) throws IOException { InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = new FileInputStream(source); outputStream = new FileOutputStream(target); Base64.encode(inputStream, outputStream); } finally { if (outputStream != null) { try { outputStream.close(); } catch (Throwable t) { ; } } if (inputStream != null) { try { inputStream.close(); } catch (Throwable t) { ; } } } } /** * <p> * Decodes data from the given source file contents and writes them in the * given target file. * </p> * * @param source * The source file, from which encoded data are read. * @param target * The target file, in which decoded data are written. * @throws IOException * If an I/O error occurs. * @since 1.3 */ public static void decode(File source, File target) throws IOException { InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = new FileInputStream(source); outputStream = new FileOutputStream(target); decode(inputStream, outputStream); } finally { if (outputStream != null) { try { outputStream.close(); } catch (Throwable t) { ; } } if (inputStream != null) { try { inputStream.close(); } catch (Throwable t) { ; } } } } /** * Copies data from a stream to another. * * @param inputStream * The input stream. * @param outputStream * The output stream. * @throws IOException * If a unexpected I/O er c266 ror occurs. */ private static void copy(InputStream inputStream, OutputStream outputStream) throws IOException { // 1KB buffer byte[] b = new byte[1024]; int len; while ((len = inputStream.read(b)) != -1) { outputStream.write(b, 0, len); } } }
实现方式二
mysql 字符集utf8的超集utf8mb4一个字符最多能有4字节,所以能支持emoji表情的存储。只要修改mysql数据库的编码格式变为utf8mb4即可具体方法可参考连接
这种方式后台不用再做额外的操作,直接正常存储即可,但是修改数据库会比较麻烦
实现方式三
通过emoji-Java解决数据库不能存储4字节表情符号参考这篇文章,这里不再叙述
相关文章推荐
- java后台处理APP表情
- App登陆java后台处理和用户权限验证
- App用户登陆 java后台处理和用户权限验证(AOP AspectJ)
- fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map
- javaScript如何处理从java后台返回的list
- java后台接受json数组中嵌套json数组的处理
- flex 处理java后台返回的JavaBean
- java后台与jsp前台特殊字符处理(字符串编码与解码)
- java发送url请求进行文件的提交以及后台struts2的action接收处理
- java后台接受app上传图片
- APP表情存入mysql数据库失败处理
- 05.Java后台如何接收并处理前台传过来的json格式的数组参数
- php 处理APP emoji表情包 IOS表情包 Mysql保存手机表情
- 支付宝APP支付(IOS手机端+java后台)版
- Java后台如何接收并处理前台传过来的json格式的数组参数
- js+ajax处理java后台返回的json对象循环创建到表格的方法
- java后台线程处理
- 【Java开发常见问题解决】文本框表情emoji字符过滤处理方法
- json数据与JAVA数据的转换 jsonJavaBean.netApache 自己编写了一个工具类,处理页面提交json格式数据到后台,再进行处理成JAVA对象数据 1、DTO:Data T
- [校园卡APP]-后台模块抽取路由过程中的错误处理(1)