大部分java开发是在Windows上开发,而生产环境下在Linux中运行.
Windows/Dos 换行符 CRLF\r\n
Linux/Unix 换行符 LF\n
MacOS 换行符 CR\r
造成问题,在win下编辑的文本,上传Linux后文件内容每行内容会多出来^M$
,在计算文本摘要和文件摘要就不一样了。
参考
Shell中去掉文件中的换行符简单方法
linux 如何删除文件中的空格和换行符号并保存到新文件中
linux删除文件中换行符的三种方法
linux统计文件每行长度,wc命令 – 统计文件的字节数、字数、行数
Windows和Linux的文件分隔符、换行符问题
问题
测试 文本test.txt
,在win下编辑内容hello word
,计算crc和md5不一致,只有截取后计算一致
[root@localhost ~]# wc -c test.txt
12 test.txt
[root@localhost ~]# cat test.txt | md5sum
c95c2e9369b537420c0d35ef7ba4f110 -
[root@localhost ~]# echo 'hello word'|md5sum
4d2220fcf2abf3a9baac712bb93bd29c -
[root@localhost ~]# cat test.txt |cksum
1699679688 12
[root@localhost ~]# echo 'hello word'|cksum
1125081862 11
[root@localhost ~]# cat test.txt | cut -b 1-10| cksum
1125081862 11
[root@localhost ~]# cat test.txt | cut -b 1-10| md5sum
4d2220fcf2abf3a9baac712bb93bd29c -
[root@localhost ~]# cat -A test.txt
hello word^M$
[root@localhost ~]# cat test.txt
hello word
在Linux上创建文件是没问题的
[root@localhost ~]# echo 'hello word'|md5sum
4d2220fcf2abf3a9baac712bb93bd29c -
[root@localhost ~]# echo 'hello word'|cksum
1125081862 11
[root@localhost ~]# echo 'hello word' > 2test.txt
[root@localhost ~]# cksum 2test.txt
1125081862 11 2test.txt
[root@localhost ~]# md5sum 2test.txt
4d2220fcf2abf3a9baac712bb93bd29c 2test.txt
[root@localhost ~]# wc -c 2test.txt
11 2test.txt
[root@localhost ~]# cat 2test.txt
hello word
[root@localhost ~]# cat -A 2test.txt
hello word$
END
所以文件要么直接在Linux创建,要么计算的时候处理一下文件内容