编码在Java中Base64和解码C#
我必须将文件发送到我的web服务,但web服务假设文件(字节数组)作为base64Binary的。
I have to send a file to my webservice, but the webservice assumes the file (byte Array) as a base64Binary.
在编码之前,byteArrayFile被保存在磁盘上的常规文件。 (我做的只是用于测试)
Before the encoding, the byteArrayFile is saved on disk as a regular File. (I'm doing it just for testing)
所以,在我的Java客户端web服务,我送的信息是这样的:
So, in my Java client for webservice, I'm sending the information this way:
String file = new sun.misc.BASE64Encoder().encode(byteArrayFile);
port.sendFileToWebService(file);
web服务必须去code中的信息,并保存在磁盘上接收的文件。
The webservice have to decode the information and save the received file on disk.
[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#文件去codeD都没有平等。
我不知道这是否是一个Java编码或C#解码的问题。
The file saved on disk (before encoding) and the file decoded with C# are not equals. I don't know if it's a problem in Java encoding or C# decoding.
任何建议,包括改变文件类型或逻辑的过程,永远是AP preciated。
Any suggestions, including changing file types or logic process, will always be appreciated.
在此先感谢!
编辑 -
文件比较:
EDIT - File comparison:
我知道XSD标准规定称为数据类型的 base64Binary的。这是什么应该允许,是你的 [的WebMethod]
参数是一个字节[]
。然后,基础服务栈将连接code中的字节数组一个base64字符串。
I know that the XSD standard specifies a data type called base64Binary. What this should allow for, is your [WebMethod]
parameter to be a byte[]
. Then the underlying service stack will encode the byte array to a base64 string.
例如,我只是做了这样一个快速Java服务
For example, I just did a quick Java service like this
@WebMethod(operationName = "TestByteArray")
public void testByteArray(byte[] data) {
}
和生成的WSDL看起来像这样的相关部分:
And the relevant parts of the generated WSDL look like this:
<operation name="TestByteArray">
<input wsam:Action="jordan.services/EncodingTests/TestByteArrayRequest" message="tns:TestByteArray"/>
<output wsam:Action="jordan.services/EncodingTests/TestByteArrayResponse" message="tns:TestByteArrayResponse"/>
</operation>
和
<xs:complexType name="TestByteArray">
<xs:sequence>
<xs:element name="arg0" type="xs:base64Binary" nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
我也做了测试在.net:
I have also done a test in .Net:
[WebMethod]
public void testByteArray(byte[] bytes) {
}
生成WSDL的相关部分:
Relevant parts of the generated WSDL:
<wsdl:portType name="TestWSSoap">
<wsdl:operation name="testByteArray">
<wsdl:input message="tns:testByteArraySoapIn"/>
<wsdl:output message="tns:testByteArraySoapOut"/>
</wsdl:operation>
</wsdl:portType>
和
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="testByteArray">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="bytes" type="s:base64Binary"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="testByteArrayResponse">
<s:complexType/>
</s:element>
</s:schema>
</wsdl:types>