我需要将一个文件发送到我的webservice,但是webservice假定文件(字节数组)是base64Binary格式。
在编码之前,byteArrayFile被保存在磁盘上作为一个普通的文件。(我只是为了测试这个功能)
所以,在我的Java客户端中,我通过以下方式发送信息:
String file = new sun.misc.BASE64Encoder().encode(byteArrayFile);
port.sendFileToWebService(file);
这个Web服务需要解码信息并将接收到的文件保存在磁盘上。
[WebMethod]
public string sendFileToWebService(string file)
{
string dirname = HttpContext.Current.Request.PhysicalApplicationPath + "\\Attachments\\";
if (!System.IO.Directory.Exists(dirname))
{
System.IO.Directory.CreateDirectory(dirname);
}
string filename = dirname + "/" + "file.sim";
WebClient myWebClient = new WebClient();
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] byteArray = null;
byteArray = Convert.FromBase64String(file.Replace("\n", ""));
byte[] responseArray = myWebClient.UploadData(filename, "POST", byteArray);
return "Webservice says OK";
}
问题是:
保存在磁盘上的文件(编码之前)和使用C#解码后的文件不相等。 我不知道这是Java编码还是C#解码的问题。
任何建议,包括更改文件类型或逻辑处理,将始终受到赞赏。
提前感谢!
编辑 - 文件比较:
原始文件链接: http://img819.imageshack.us/img819/820/originalu.png 解码后的文件链接(经过Java编码): http://img826.imageshack.us/img826/3184/processed.png