编解码
ASCII (American Standard Code for Information Interchange 美国信息交互标准编码)
采用 7 个 bit 来表示一个字符,共计可可以表示 128中字符
ISO-8859-1 完全兼容 ASCII
采用 8 bit 来表示一个字符,即用一个 字节(byte)来表示一个字符
GBK 两个字节表示一个汉字(GBK 、GBK18030)
BIG5 (台湾繁体)
Unicode 全球最为广泛的一种编码方式,但是存在存储占用空间较大,浪费存储资源,并不适合以存储,
采用 2 个字节(byte)来表示一个字符,
UTF :Unicode Translation Format ,Unicode 转换格式,UTF 是一种存储格式
UTF-8、UTF-16 、UTF-32 都是 Unicode的实现方式之一
UTF-16:UTF-16-LE (little endian 小端),UTF-16-BE (big endian 大端),是一种定长的存储方式,2 个字节存储一个字符,存储浪费
Zero With NO-Break Space,零宽度不换行的空格,0xFEFF (BE),0xFFFE (LE),即在一个文件的开始加上这个占位符,表示大端小端
UTF-8:变长的字节表示形式,如 ASCII 码的 128个字符,在UTF-8 中表示形式和 ASCII 是一模一样的 兼容 ISO-8859-1 、ASCII,
一般来说 UTF-8 用三个字节表示一个中文,UTF-8最多可用 6 个字节表示一个字符
BOM (Byte Order Mark 字节序),如大端小端 就是一种 BOM 本质上来说 UTF-8 是不需要 BOM的,BOM 常出现 Windows 系统中,历史遗留问题
public class CharsetTest { public static void main(String[] args) throws IOException { String inPutPath = "java-nio/src/main/resource/CharsetTest.txt"; String outPutPath = "java-nio/src/main/resource/OutCharsetTest.txt"; RandomAccessFile inFile = new RandomAccessFile(inPutPath, "r"); RandomAccessFile ourFile = new RandomAccessFile(outPutPath, "rw"); File file = new File(inPutPath); long fileLength = file.length(); FileChannel inChannel = inFile.getChannel(); FileChannel outChannel = ourFile.getChannel(); MappedByteBuffer inputData = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength); System.err.println("-------------- 查看计系统所支持的字符集 ---------------------"); Charset.availableCharsets().forEach((k, v) -> { System.err.println(k + "," + v); }); Charset charset = Charset.forName("utf-8"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); // 进行编解码 CharBuffer inCharBuffer = decoder.decode(inputData); ByteBuffer outByteBuffer = encoder.encode(inCharBuffer); // 写入输出数据 outChannel.write(outByteBuffer); inFile.close(); ourFile.close(); } }
开发中工具编写收集,请忽略以下内容。。。。
图片转码 Base64
import org.apache.commons.codec.binary.Base64; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;
public class ImageConvert { /** * 网络文件得到字节数组 * @param picUrl * @return */ public static byte[] net2ByteArray(String picUrl) { InputStream inputStream = null; byte[] resultBytes = null; try { //创建url URL url = new URL(picUrl); byte[] bytes = new byte[1024]; //创建连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); inputStream = connection.getInputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 4]; int len = 0; /* while (-1 != (len = inputStream.read(buffer))) { outputStream.write(buffer, 0, len); } */ while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } resultBytes = outputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { //关流 inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return resultBytes; }
/** * 本地文件转字节数组 * @param picPath * @return */ public static byte[] local2ByteArray(String picPath) { FileInputStream inputStream = null; byte[] bytes = null; try { inputStream = new FileInputStream(picPath); //定义数组的长度 int length = inputStream.available(); bytes = new byte[length]; inputStream.read(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return bytes; } /** * base64 字符串转 byte[] * * @param base64Str * @return */ public static byte[] bs64Str2Byte(String base64Str) { byte[] bytes = Base64.decodeBase64(base64Str); return bytes; } /** * byte[]转base64 * * @param bytes * @return */ public static String byte2Bs64Str(byte[] bytes) { //对字节数组进行base64 编码 //BASE64Encoder base64Encoder = new BASE64Encoder(); //String base64Str = base64Encoder.encode(bytes); String base64Str = Base64.encodeBase64String(bytes); return base64Str; } }
获取文件MD5值及 大小
String md5Hex = DigestUtils.md5Hex(file.getInputStream()); BigDecimal appSize = new BigDecimal(file.getSize()); // 单位字节 appSize = appSize.divide(new BigDecimal(1024 * 1024), 2, BigDecimal.ROUND_HALF_UP); resultMap.put("appSize", appSize + "MB"); resultMap.put("md5", md5Hex);
本文来自博客园,作者:Vermeer,转载请注明原文链接:https://www.cnblogs.com/chxlay/p/15056855.html