日期:2014-05-17  浏览次数:20775 次

java通过socket通讯发送excel文件到c#端,excel文件乱码
java端socket,发送excel文件字节流
	public boolean sendExcel() {
Socket socket;
ServerSocket ss;
DataOutputStream ps;
byte[] sendBytes = null;
try {
ss = new ServerSocket(this.port);
socket = ss.accept();
ps = new DataOutputStream(socket.getOutputStream());
fileinputstream = new FileInputStream(this.file);
sendBytes = new byte[fileinputstream.available()];
while (true) {
int read = 0;
if (fileinputstream != null) {
read = fileinputstream.read(sendBytes);
}
if (read == -1) {
break;
}
ps.write(sendBytes, 0, read);
}
Thread.sleep(5000);
ps.flush();
System.out.println("文件发送完成");
return true;
} catch (Exception e1) {
e1.printStackTrace();
return false;
}
}


c#客户端(控制台),接收excel字节流,并保存为文件

 
   class Program
    {
        static void Main(string[] args)
        {
            string host;
            int port;
            string path;
            try
            {
            //int port = 2000;
            //string host = "127.0.0.1";
            lable:
                Console.Write("设定服务端ip:");
                //"127.0.0.1:2000"
                // host = Console.ReadLine();
                host = "192.168.0.221";
                Console.Write("设定服务端port:");
                //port = Int32.Parse(Console.ReadLine());