\r\n不替换成\r\r\n

This commit is contained in:
jiangzhongxiang 2019-06-15 17:57:09 +08:00
parent 53ac565a28
commit 13e4d4361b
1 changed files with 69 additions and 46 deletions

View File

@ -1,6 +1,5 @@
package com.educoder.bridge.common.utils;
import org.apache.commons.codec.binary.Base64;
import java.nio.charset.StandardCharsets;
@ -10,54 +9,78 @@ import java.nio.charset.StandardCharsets;
*/
public class Base64Util {
/**
* base64编码
*
* @param code
* @return
*/
public static String encode(String code) {
byte[] encode = Base64.encodeBase64URLSafe(code.getBytes(StandardCharsets.UTF_8));
return new String(encode, StandardCharsets.UTF_8);
}
/**
* base64编码
*
* @param code
* @return
*/
public static String encode(String code) {
byte[] encode = Base64.encodeBase64URLSafe(code.getBytes(StandardCharsets.UTF_8));
return new String(encode, StandardCharsets.UTF_8);
}
public static byte[] encodeBytes(byte[] codes) {
return Base64.encodeBase64(codes);
}
public static byte[] encodeBytes(byte[] codes) {
return Base64.encodeBase64(codes);
}
/**
* base64解码
*
* @param code
* @return
*/
public static String decode(String code) {
byte[] decode = Base64.decodeBase64(code);
return new String(decode, StandardCharsets.UTF_8);
}
/**
* base64解码
*
* @param code
* @return
*/
public static String decode(String code) {
byte[] decode = Base64.decodeBase64(code);
return new String(decode, StandardCharsets.UTF_8);
}
/**
* base64再解码把原本的非URL safe编码转换为URL safe编码
*
* @param code
* @return
*/
public static String reencode(String code) {
String str = decode(code);
//str = str.replace("\n", "\r\n");
str = replaceLineFeed(str);
return encode(str);
}
private static String replaceLineFeed(String str) {
StringBuilder sb = new StringBuilder();
char[] cs = str.toCharArray();
int len = cs.length;
for (int i = 0; i < len; i++) {
char c = cs[i];
if (c == '\n') {
if (i == 0) {
sb.append('\r').append(c);
} else {
char pre = cs[i - 1];
if (pre == '\r') {
sb.append(c);
} else {
sb.append('\r').append(c);
}
}
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* base64再解码把原本的非URL safe编码转换为URL safe编码
*
* @param code
* @return
*/
public static String reencode(String code) {
String str = decode(code);
str = str.replace("\n", "\r\n");
return encode(str);
}
/**
* 正常编码
*
* @param code
* @return
*/
public static String encodeNoSafe(String code) {
byte[] encode = Base64.encodeBase64(code.getBytes(StandardCharsets.UTF_8));
return new String(encode, StandardCharsets.UTF_8);
}
/**
* 正常编码
*
* @param code
* @return
*/
public static String encodeNoSafe(String code) {
byte[] encode = Base64.encodeBase64(code.getBytes(StandardCharsets.UTF_8));
return new String(encode, StandardCharsets.UTF_8);
}
}