在导出到JAR时,从套接字读取编码字符不起作用

时间:2016-07-23 15:39:50

标签: java eclipse

如果我在eclipse中作为java应用程序运行没有问题但是如果我导出为可运行的jar文件项目我得到编码问题?

如果它从eclipse运行,我可以通过套接字读取来自服务器的字符“ş”。如果它作为一个可运行的jar运行,我无法阅读char“ş”

我该如何解决这个问题?

修改

public class Start {

     public static void main(String[] args) {


               byte[] buff = new byte[1024];
               Socket socket = null;
               InputStream in = null;

               try {
                    socket = new Socket("11.11.11.11");
                    in = socket.getInputStream();

               } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
               }

               try {
                    in.read(buff);

               } catch (IOException e) {
                     e.printStackTrace();
               } 

               System.out.println(new String(buff));




    }

}

1 个答案:

答案 0 :(得分:2)

[api-server] $ console
[info] Starting scala interpreter...
[info]
2016-07-23 11:11:01,106 [INFO] [p.a.d.DefaultDBApi] - Database [default] connected at jdbc:postgresql://localhost:5432/databasename
import play.api.{ApplicationLoader, Environment, Mode}
import com.projectname.apiserver.global.MacwireApplicationLoader
import com.projectname.apiserver.model._
env: play.api.Environment = Environment(.,scala.tools.nsc.interpreter.IMain$TranslatingClassLoader@5b0c7029,Dev)
context: play.api.ApplicationLoader.Context = Context(Environment(.,scala.tools.nsc.interpreter.IMain$TranslatingClassLoader@5b0c7029,Dev),None,play.core.DefaultWebCommands@352ca33b,Configuration(Config(SimpleConfigObject({"akka":{"actor":{"creation-timeout":"20s","debug":{"autoreceive":"off","event-stream":"off","fsm":"off","lifecycle":"off","receive":"off","router-misconfiguration":"off","unhandled":"off"},"default-dispatcher":{"attempt-teamwork":"on","default-executor":{"fallback":"fork-join-executor"},"executor":"defa...Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions for evaluation. Or try :help.

scala> crypto.encryptAES("test")
res0: String = 2-ZLIW79WqQff4SDZ+aWLkf38cZyU=

使用当前平台的默认编码创建字符串。这在Eclipse和Eclipse之外可能有所不同。

改为使用

new String(buff)

指定字节数组应始终被视为UTF-8(假设它是您正在接收的UTF-8)

另外别忘了关闭你的溪流。