【发布时间】:2012-08-24 12:36:27
【问题描述】:
我正在试验我们在生产中看到的边缘案例。我们有一个业务模型,客户端生成文本文件,然后将它们通过 FTP 传输到我们的服务器。我们摄取这些文件并在我们的 Java 后端(在 CentOS 机器上运行)处理它们。我们的大多数 (95%+) 客户都知道以 UTF-8 格式生成这些文件,这正是我们想要的。然而,我们有一些顽固的客户端(但大帐户)在 Windows 机器上使用 CP1252 字符集生成这些文件。不过没问题,我们已经配置了我们的 3rd 方库(这是我们的大部分“处理”工作)以通过一些神奇的 voo doo 处理任何字符集的输入。
有时,我们会看到一个文件名中包含非法 UTF-8 字符 (CP1252)。当我们的软件尝试从 FTP 服务器读取这些文件时,正常的文件读取方法会阻塞并抛出 FileNotFoundException:
File f = getFileFromFTPServer();
FileReader fReader = new FileReader(f);
String line = fReader.readLine();
// ...etc.
异常如下所示:
java.io.FileNotFoundException: /path/to/file/some-text-blah?blah.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) at
java.io.FileInputStream.(FileInputStream.java:120) at java.io.FileReader.(FileReader.java:55) at com.myorg.backend.app.InputFileProcessor.run(InputFileProcessor.java:60) at
java.lang.Thread.run(Thread.java:662)
所以我认为正在发生的事情是因为文件 name 本身包含非法字符,我们甚至一开始就无法读取它。如果可以,那么无论文件的内容如何,我们的软件都应该能够正确处理它。所以这确实是读取包含非法 UTF-8 字符的文件名的问题。
作为一个测试用例,我创建了一个非常简单的 Java“应用程序”来部署在我们的一个服务器上并测试一些东西(源代码在下面提供)。然后我登录到一台 Windows 机器并创建了一个测试文件并将其命名为test£.txt。注意文件名中“test”后面的字符。这是 Alt-0163。我通过 FTP 将它发送到我们的服务器,当我在其父目录上运行 ls -ltr 时,我惊讶地发现它被列为 test?.txt。
在我继续之前,这是我为测试/重现此问题而编写的 Java“应用程序”:
public Driver {
public static void main(String[] args) {
Driver d = new Driver();
d.run(args[0]); // I know this is bad, but its fine for our purposes here
}
private void run(String fileName) {
InputStreamReader isr = null;
BufferedReader buffReader = null;
FileInputStream fis = null;
String firstLineOfFile = "default";
System.out.println("Processing " + fileName);
try {
System.out.println("Attempting UTF-8...");
fis = new FileInputStream(fileName);
isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
buffReader = new BufferedReader(isr);
firstLineOfFile = buffReader.readLine();
System.out.println("UTF-8 worked and first line of file is : " + firstLineOfFile);
}
catch(IOException io1) {
// UTF-8 failed; try CP1252.
try {
System.out.println("UTF-8 failed. Attempting Windows-1252...(" + io1.getMessage() + ")");
fis = new FileInputStream(fileName);
// I've also tried variations "WINDOWS-1252", "Windows-1252", "CP1252", "Cp1252", "cp1252"
isr = new InputStreamReader(fis, Charset.forName("windows-1252"));
buffReader = new BufferedReader(isr);
firstLineOfFile = buffReader.readLine();
System.out.println("Windows-1252 worked and first line of file is : " + firstLineOfFile);
}
catch(IOException io2) {
// Both UTF-8 and CP1252 failed...
System.out.println("Both UTF-8 and Windows-1252 failed. Could not read file. (" + io2.getMessage() + ")");
}
}
}
}
当我从终端 (java -cp . com/Driver t*) 运行它时,我得到以下输出:
Processing test�.txt
Attempting UTF-8...
UTF-8 failed. Attempting Windows-1252...(test�.txt (No such file or directory))
Both UTF-8 and Windows-1252 failed. Could not read file.(test�.txt (No such file or directory))
test�.txt?!?!我做了一些研究,发现“�”是Unicode替换字符\uFFFD。所以我猜发生的事情是 CentOS FTP 服务器不知道如何处理 Alt-0163 (£),所以它用 \uFFFD (�) 替换它。但我不明白为什么ls -ltr 显示一个名为test?.txt 的文件...
无论如何,解决方案似乎是添加一些逻辑来搜索文件名中是否存在此字符,如果找到,则将文件重命名为其他名称(例如可能执行字符串方式 @987654338 @ 或类似的东西)系统可以读取和处理。
问题是Java 甚至没有看到文件系统上的这个文件。 CentOS 知道该文件在那里 (test?.txt),但是当该文件被传递到 Java 时,Java 将其解释为 test�.txt 并且出于某种原因 No such file or directory...
我怎样才能让 Java 看到这个文件,以便我可以对它执行File::renameTo(String)?很抱歉这里的背景故事,但我觉得它是相关的,因为在这种情况下每个细节都很重要。提前致谢!
【问题讨论】:
-
所以你不能列出目录中的文件,然后查看哪些名称中有“奇数字符”并使用 file.renameTo 将它们重命名为“timestamp+random.something”?
-
@MarkusMikkolainen - 您是在说手动执行此操作吗?如果不是,您指的是什么语言/脚本?
-
我建议您使用 File 对象而不是传递文件名。这可能会防止任何文件名损坏。
-
我的意思是你会使用 java new File(parentdir).listFiles 将该目录中的 File 对象列为 File[],然后使用这些 File 对象来处理文件而不是传递文件名.如果您需要使用文件名,则使用 File 对象之一来更改有问题的文件的名称 iwth file.renameTo("reasonable.txt");
-
打电话给
java.io.File#listFiles()怎么样?它可能会返回对此类文件的引用。 docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles()
标签: java utf-8 character-encoding filenotfoundexception windows-1252