Reputation: 19617
I've written this little test class to connect up to an FTP server.
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:[email protected]");
} 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();
}
}
}
Here's the output:
-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
Notice the name of the directory at the end of the list. There should be an "é" (e with acute accent) instead of the double character "é".
This reminds me of an issue encountered previously with JSF where there was a mix-up between standards. I have little experience with character-encoding though so I'm not sure what's happening. I'm supposing that the server output is in ASCII so how do I adapt the output so it appears correctly in the console?
Upvotes: 3
Views: 807
Reputation: 67760
You're brute-force converting byte
s from the input stream into char
s using
char c = (char) b;
This is definitely not the Good Housekeeping approved form.
Stream
s deliver byte
s, and you want char
s. Reader
s deliver char
s and will do character set translation for you in an automatic and controlled way.
You should wrap an InputStreamReader
around the InputStream
. The constructor for InputStreamReader
allows you to specify a CharSet
, which will let you control the translation.
Reading from the InputStreamReader
will of course yield "real" char
s. Another benefit is that you can wrap a BufferedReader
around the InputStreamReader
and then read entire lines at a time (into a String
) using readLine
.
EDIT: To illustrate what I mean by "wrap around," here's some (untested!) coding to illustrate the idea:
BufferedReader br = new BufferedReader(new InputStreamReader(bin, "US-ASCII"));
...
String line = br.readLine();
Upvotes: 2