我编写了这个小测试类来连接到 FTP 服务器。
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class FTPTest {
public static void main(String[] args) {
URL url = null;
try {
url = new URL("ftp://anonymous:Password@127.0.0.1");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection conn = null;
try {
conn = url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
InputStream in = null;
try {
in = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream bin = new BufferedInputStream(in);
int b;
try {
while ((b = bin.read()) != -1) {
char c = (char) b;
System.out.print("" + (char) b);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是输出:
-rw-r--r-- 1 ftp ftp 4700 Apr 30 2007 premier.java
-rw-r--r-- 1 ftp ftp 88576 Oct 23 2007 Serie1_1.doc
-rw-r--r-- 1 ftp ftp 1401 Nov 21 2006 tp20061121.txt
drwxr-xr-x 1 ftp ftp 0 Apr 23 20:04 répertoire
注意列表末尾的目录名称。应该有一个“é”(带有尖锐重音的 e)而不是双字符“é”。
这让我想起了以前在 JSF 中遇到的一个问题,即标准之间的混淆。我对字符编码几乎没有经验,所以我不确定发生了什么。我假设服务器输出是 ASCII 格式,那么如何调整输出以使其正确显示在控制台中?