2014-01-13 197 views
0

我有一個簡單的Java程序:瑞典字符損壞

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import org.apache.commons.exec.CommandLine; 
import org.apache.commons.exec.DefaultExecuteResultHandler; 
import org.apache.commons.exec.DefaultExecutor; 
import org.apache.commons.exec.ExecuteException; 
import org.apache.commons.exec.LogOutputStream; 
import org.apache.commons.exec.PumpStreamHandler; 

public class test { 

public static void main(String[] args) { 

CommandLine commandLine = new CommandLine("c:/echo.bat"); 
/*CommandLine commandLine = new CommandLine("cmd.exe "); 
commandLine.addArgument("/c"); 
commandLine.addArgument(" echo Båt"); */ 

DefaultExecutor executor = new DefaultExecutor(); 
try { 

LogOutputStream output = new LogOutputStream() { 
@Override 
protected void processLine(String line, int level) { 
System.out.println(line); 
} 
}; 
PumpStreamHandler streamHandler = new PumpStreamHandler(output); 
executor.setStreamHandler(streamHandler); 

executor.setExitValue(0); 

DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); 
executor.execute(commandLine, resultHandler); 

} catch (ExecuteException ex) { 
ex.printStackTrace(); 
} catch (IOException ex) { 
ex.printStackTrace(); 
} 
} 
} 

如果我運行這個從含有批處理文件「回聲BAT」,輸出正確地顯示:

Ç :>回波BAT
BAT

我若運行 「CMD/C回波BAT」,輸出被損壞:

乙†噸

任何人有任何想法?謝謝。

在看到Jouni Aro的評論(謝謝!)後,我通過使用'-Dfile.encoding ='設置進行了更多測試。而這裏的結果:

1)未設置(使用系統默認):

字符集是:窗口1252

C:>回聲蝙蝠
蝙蝠

字符集是:窗口-1252

乙†噸

2) '-Dfile.encoding = UTF8':

Charset是:UTF-8

C:>回波B�t
B�t

Charset是:UTF-8

B�t

3)「 - 指定-Dfile.encoding = ISO-8859-1' :

Charset是:ISO-8859-1

C:>回聲蝙蝠
蝙蝠

字符集是:ISO-8859-1

乙†牛逼

所以看起來字符集是沒有幫助嗎?

+0

所不同的是在代碼頁。在批處理文件中,charset與cmd.exe將使用的相同,但是當您使用Java編寫命令時,它使用的字符集與cmd.exe的默認字符集不匹配。 但我不確定,CommandLine對象的字符集將用於字符串,或者如果您可以將字符串轉換爲正確的字符集。 –

回答

0

試用java "B\u00e5t"。問題可能是編輯器(java源代碼)的編碼與用於javac編譯器的編碼不同。

如果在 「UTF-8」 編輯那它一定是

javac -encoding UTF-8 ... 
+0

'B \ u00e5t' 的結果是相同的: 字符集是:UTF-8 B�t 字符集是:ISO-8859-1 乙†噸 Charset是:窗口1252 乙†噸 – user3191618

+0

我我確信你應該嘗試'Cp850',這是DOS代碼頁,帶'å'。由Windows命令行使用。 –

+0

字符集是:IBM850 BankL†網 – user3191618

0
/** 
* This file created at 2016年1月11日. 
* 
* Copyright (c) 2002-2016 Honey, Inc. All rights reserved. 
*/ 
package com.honey.proxy.helper; 

import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PipedInputStream; 
import java.io.PipedOutputStream; 
import java.util.List; 
import java.util.Properties; 

import org.apache.commons.exec.CommandLine; 
import org.apache.commons.exec.DefaultExecutor; 
import org.apache.commons.exec.ExecuteException; 
import org.apache.commons.exec.ExecuteWatchdog; 
import org.apache.commons.exec.Executor; 
import org.apache.commons.exec.PumpStreamHandler; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import com.honey.proxy.exception.CmdRunException; 

/** 
* <code>{@link AbstractCommonExecs}</code> 
* 
* TODO : document me 
* 
* @author Honwhy 
*/ 
public abstract class AbstractCommonExecs { 

    private Logger log = LoggerFactory.getLogger(AbstractCommonExecs.class); 
    private static final String DEFAULT_ENCODING = "UTF-8"; 
    private String encoding = DEFAULT_ENCODING; 

    private String bin; 
    private List<String> arguments; 
    public AbstractCommonExecs(String bin, List<String> arguments) { 
     this.bin = bin; 
     this.arguments = arguments; 
    } 

    public ExecResult exec() throws CmdRunException{ 
     ExecResult er = new ExecResult(); 
     try { 
      Executor executor = getExecutor(); 
      CommandLine cmdLine = getCommandLine(); 
      log.info("Executing script {}",cmdLine.toString()); 
      if(supportWatchdog()) { 
       executor.setWatchdog(getWatchdog()); 
      } 
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
      PipedOutputStream outputStream = new PipedOutputStream(); 
      PipedInputStream pis = new PipedInputStream(outputStream); 
      //ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); 
      PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream,errorStream); 
      executor.setStreamHandler(streamHandler); 
      int ret = executor.execute(cmdLine); 

      BufferedReader br = new BufferedReader(new InputStreamReader(pis, getEncoding())); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while((line = br.readLine()) != null) { 
       sb.append(line+"\n"); 
      } 
      pis.close(); 
      String stdout = sb.toString(); 
      //String stdout = outputStream.toString(getEncoding()); 
      String stderr = errorStream.toString(getEncoding()); 
      er.setStderr(stderr); 
      log.info("output from script {} is {}", this.bin, stdout); 
      log.info("error output from script {} is {}", this.bin, stderr); 
      log.info("exit code from script {} is {}", this.bin, ret); 
      er.setStdout(stdout); 
      er.setExitCode(ret); 
      return er; 
     } catch (ExecuteException e) { 
      throw new CmdRunException("", e); 
     } catch (IOException e) { 
      throw new CmdRunException("", e); 
     } 

    } 

    public Executor getExecutor() { 
     Executor executor = new DefaultExecutor(); 
     executor.setWorkingDirectory(new File(this.bin).getParentFile()); 
     return executor; 
    } 
    public CommandLine getCommandLine() { 
     String fullCommand = bin + join(arguments); 
     return CommandLine.parse(fullCommand); 
    } 
    protected String join(List<String> arguments) { 
     if(arguments == null || arguments.isEmpty()) { 
      return ""; 
     } 
     StringBuilder sb = new StringBuilder(); 
     for(String arg : arguments) { 
      sb.append(" ").append(arg); 
     } 
     return sb.toString(); 
    } 

    /** 
    * @return the encoding 
    */ 
    public String getEncoding() { 
     return encoding; 
    } 

    /** 
    * @param encoding the encoding to set 
    */ 
    public void setEncoding(String encoding) { 
     this.encoding = encoding; 
    } 

    public abstract boolean supportWatchdog(); 
    public abstract ExecuteWatchdog getWatchdog(); 
} 

//===================== 
// Test class 
/** 
* This file created at 2016年1月12日. 
* 
* Copyright (c) 2002-2016 Honey, Inc. All rights reserved. 
*/ 
package com.honey.command; 

import java.util.ArrayList; 
import java.util.List; 

import org.apache.commons.exec.ExecuteWatchdog; 

import com.honey.proxy.helper.AbstractCommonExecs; 
import com.honey.proxy.helper.ExecResult; 

/** 
* <code>{@link GbkCommonExecs}</code> 
* 
* windows開發環境測試 
* 
* @author Honwhy 
*/ 
public class GbkCommonExecs extends AbstractCommonExecs{ 

    /** 
    * @param bin 
    * @param arguments 
    */ 
    public GbkCommonExecs(String bin, List<String> arguments) { 
     super(bin, arguments); 
    } 

    /* (non-Javadoc) 
    * @see com.bingosoft.proxy.helper.AbstractCommonExecs#supportWatchdog() 
    */ 
    @Override 
    public boolean supportWatchdog() { 
     // TODO implement AbstractCommonExecs.supportWatchdog 
     return false; 
    } 

    /* (non-Javadoc) 
    * @see com.bingosoft.proxy.helper.AbstractCommonExecs#getWatchdog() 
    */ 
    @Override 
    public ExecuteWatchdog getWatchdog() { 
     // TODO implement AbstractCommonExecs.getWatchdog 
     return null; 
    } 

    public String getEncoding() { 
     return "GBK"; 
    } 
    public static void main(String[] args) { 
     String bin = "ping"; 
     String arg1 = "127.0.0.1"; 
     List<String> arguments = new ArrayList<String>(); 
     arguments.add(arg1); 
     AbstractCommonExecs executable = new GbkCommonExecs(bin, arguments); 
     ExecResult er = executable.exec(); 
     System.out.println(er.getExitCode()); 
     System.out.println(er.getStdout()); 
     System.out.println(er.getStderr()); 
    } 

}