我有一个文件,我的 java 代码在作业中读取并在这一行中将编码设置为 UTF8:
LineNumberReader fin = new LineNumberReader(new InputStreamReader(new FileInputStream(mutationInputFileName), StandardCharsets.UTF_8));
在这一行之后我检查了编码:detectEncoding(mutationInputFileName);
当我在我的 mac 上本地运行作业时,文件的编码是 UTF8。当我在部署机器上运行代码时,代码是 ASCII。 我正在使用 tika 来检测文件编码:
public void detectEncoding(String filePath) {
try (FileInputStream fis = new FileInputStream(filePath);
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis)) {
String encoding = isr.getEncoding();
System.out.println("Detected encoding of file: " + filePath + " is : " + encoding);
} catch (IOException e) {
e.printStackTrace();
}
}
我已经添加了:
System.out.println("System encoding: "+System.getProperty("file.encoding"));
查看更多,在我的本地机器上它仍然是 utf8,在部署机器上它是:系统编码:ANSI_X3.4-1968。部署机器使用 linux,我在两台机器上都使用 java 8。
更奇怪的是,如果在终端中我检查编码: file -bi text1.txt 在我的本地机器上说:“常规文件”但在部署版本上:“text/plain; charset = utf-8”。 我设法通过给
在部署的机器上将编码设置为UTF8-Dfile.encoding=UTF-8
作为运行课程时程序的参数。
知道为什么 LineNumberReader fin = new LineNumberReader(new InputStreamReader(new FileInputStream(mutationInputFileName), StandardCharsets.UTF_8)) 没有在部署机器上将编码设置为 utf-8 吗?
-- 添加更多信息 我说的是同一个文件,比方说 file1.txt。我已经从部署机器下载了文件。该文件是由我无法访问的另一个进程创建的。步骤是:
String signature = encodeAsMD5(signatureBeforeEncoding)
其中 signatureBeforeEncoding 是 col[0] 和 :
public static String encodeAsMD5(String value) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
byte[] messageDigest = md.digest(value.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
return number.toString(16);
}
我在本地机器上得到的签名与开发者不同。如果我通过了,我会得到相同的签名
-Dfile.encoding=UTF-8
作为运行课程时程序的参数。