在CSHARP服务器上保存JAVA readed Image

时间:2016-01-02 08:35:40

标签: java c# tcp server client

我正在尝试从我的CSHARP-SERVER上的JAVA-CLIENT中保存图像。 我做了以下事情:

  1. 以字节数组(JAVA-CLIENT)
  2. 读取图像
  3. 将字节数组转换为base64字节数组(JAVA-CLIENT)
  4. 将base64字节数组发送到CSHARP-SERVER(JAVA-CLIENT)
  5. 接收base64字节数组(CSHARP-SERVER)
  6. base64字节数组到base64字符串(CSHARP-SERVER)
  7. 将base64字符串解码为utf8字符串(CSHARP-SERVER)
  8. 获取utf8字符串字节(CSHARP-SERVER)
  9. 将字节保存为图像(CSHARP-SERVER)
  10. 现在我无法在Windows imageviewer中打开此图像文件..

    但我注意到以下事项:

    • csharp服务器上的base64字符串与我的客户端
    • 相同
    • 我比较了两个文件(客户端和服务器)..在记事本中打开它们,看到它们不相等..

    如何在我的服务器上保存此图像? 做这个的最好方式是什么? 是否有开源的java-csharp-bridge库?

    这是我的代码:

    public class Program {
    
    
    static String getBase64StringFromBytes(byte[] bytes)
    {
        String b64 = null;
        try {
            //To Base64-String
            b64 = new String(Base64.getEncoder().encode(bytes),"UTF-8");
            //UTF-8
            return b64;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    
    }
    
    public static void main(String args[]) {
        //1.Read image as byte array
        byte[] data = FileManager.readeFile();
        //2.1 Convert byte array to base64 string
        String str64 = getBase64StringFromBytes(data);
        //2.2 Convert base64 string to base64 byte array
        byte[] b64Bytes = str64.getBytes();
        //3.Send base64 byte array to csharp
        Sender.sendData(b64Bytes);
    }
    

    }

    CSHARP-服务器:

    class Program
    {
        static string getStringFromBase64Bytes(byte[] base64Bytes)
        {
            //5.Base64 byte array to base64 string   
            string s64 = Encoding.UTF8.GetString(base64Bytes);      
            //6.Decode base64 string to utf string
            string utf8 = Encoding.UTF8.GetString(Convert.FromBase64String(s64)); //Base64-String to UTF-8 String      
            return utf8;
        }
    
        static void Main(string[] args)
        {
            Receiver mReceiver = new Receiver();
            //4.Receive base64 byte array
            byte[] data = mReceiver.waitForData();      
    
            string strDataTest = getStringFromBase64Bytes(data);
            //7.Get UTF-8 bytes
            byte[] fData = Encoding.UTF8.GetBytes(strDataTest);
            Console.WriteLine("DATA SIZE: " + fData.Length);
            //8.Save file
            File.WriteAllBytes("mFile.png", fData);
    
            mReceiver.closeAll();
            Console.ReadLine();
    
        }
    }
    

0 个答案:

没有答案