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: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();
}
}
}
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?
You’re brute-force converting
bytes from the input stream intochars usingThis is definitely not the Good Housekeeping approved form.
Streams deliverbytes, and you wantchars.Readers deliverchars and will do character set translation for you in an automatic and controlled way.You should wrap an
InputStreamReaderaround theInputStream. The constructor forInputStreamReaderallows you to specify aCharSet, which will let you control the translation.Reading from the
InputStreamReaderwill of course yield “real”chars. Another benefit is that you can wrap aBufferedReaderaround theInputStreamReaderand then read entire lines at a time (into aString) usingreadLine.EDIT: To illustrate what I mean by “wrap around,” here’s some (untested!) coding to illustrate the idea: