我正在尝试从我的CSHARP-SERVER上的JAVA-CLIENT中保存图像。 我做了以下事情:
现在我无法在Windows imageviewer中打开此图像文件..
但我注意到以下事项:
如何在我的服务器上保存此图像? 做这个的最好方式是什么? 是否有开源的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();
}
}