简体   繁体   中英

Java - Problems with "ü/ä/ö" after SCP

I create a Programm which can load local or remote log files. If i load a local file there is no error. But if I copy first the file with SCP to my local (where i use this code: http://www.jcraft.com/jsch/examples/ScpFrom.java.html ) and read it out I get an Error and the letters "ü/ä/ö" shown as . How can i fix this ?

Remote : Linux-Server Local: Windows-PC

Code for SCP :

http://www.jcraft.com/jsch/examples/ScpFrom.java.html

Code for reading out :

protected void openTempRemoteFile() throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( lfile )));
        String strLine;

        DefaultTableModel dtm = new DefaultTableModel(0, 0);
        String header[] = new String[]{ "Timestamp", "Session-ID", "Log" };
        dtm.setColumnIdentifiers(header);
        table.setModel(dtm);

        while ((strLine = reader.readLine()) != null) {     

            String[] sparts = strLine.split(" ");
            String[] bparts = strLine.split("   : ");

            String Timestamp = sparts[0] + " " + sparts[1];
            String SessionID = sparts[4];
            String Log = bparts[1];

            dtm.addRow(new Object[] {Timestamp, SessionID, Log});
        }
        reader.close();
}

EDIT :

Encoding Format of the Local-Files: UTF-8

Encoding Format of the SCP-Remote-Files from Linux-Server: WINDOWS-1252

Supply appropriate Charset to InputStreamReader constructor, eg:

import java.nio.charset.StandardCharsets;

...

BufferedReader reader = new BufferedReader(
    new InputStreamReader(
        new FileInputStream( lfile ),
        StandardCharsets.UTF_8)); // try also ISO_8859_1 if UTF_8 doesn't help.

To fix your problem you have at least two options:

You can specify the encoding for your files directly in your code, updating it as follow:

BufferedReader reader = new BufferedReader(
    new InputStreamReader(
        new FileInputStream( lfile ),
        "UTF8"
    )
);

or set the default file encoding when starting the JVM with:

java -Dfile.encoding=UTF-8 … com.example.Main

I definitely prefer the first way and you can parametrize the "UTF8" value too, if you need. With the latter way you could still face the same issues if you forgot to specify that.

You can replace the encoding with whatever you prefer (Refer to https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html for Supported Encodings) and, on Windows, "Cp1252" is usually the default encoding.

Remember, you can always use query the file.encoding property or Charset.defaultCharset() to find the current default encoding for your application, eg:

byte [] byteArray = {'blablabla'};
InputStream inputStream = new ByteArrayInputStream(byteArray);
InputStreamReader reader = new InputStreamReader(inputStream);
String defaultEncoding = reader.getEncoding();

Working with encoding is very tricky thing. If your system always uses this kind of files (from different environment) than you should first detect the charset than read it with given charset. I had similar problem and i used juniversalchardet to detect charset and used InputStreamReader(stream, Charset) . In your case it would be like

protected void openTempRemoteFile() throws IOException {
        String encoding = UniversalDetector.detectCharset(lfile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( lfile ), Charset.forName(encoding)));
        ....

If it is only one time job than open it in text editor (notapad++ for example) than save it in your encoding. Than use it in program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM