I have to send a file to my webservice, but the webservice assumes the file (byte Array) as a base64Binary.
Before the encoding, the byteArrayFile is saved on disk as a regular File. (I'm doing it just for testing)
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);
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";
}
The problem is:
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.
Any sug开发者_开发知识库gestions, including changing file types or logic process, will always be appreciated.
Thanks in advance!
EDIT - File comparison:
Original File http://img819.imageshack.us/img819/820/originalu.png
Decoded File (after Java encoding) http://img826.imageshack.us/img826/3184/processed.png
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.
For example, I just did a quick Java service like this
@WebMethod(operationName = "TestByteArray")
public void testByteArray(byte[] data) {
}
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>
And
<xs:complexType name="TestByteArray">
<xs:sequence>
<xs:element name="arg0" type="xs:base64Binary" nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
I have also done a test in .Net:
[WebMethod]
public void testByteArray(byte[] bytes) {
}
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>
And
<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>
Try use normal file i/o instead of a WebClient
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";
byte[] byteArray = Convert.FromBase64String(file);
File.WriteAllBytes(filename, byteArray ); //might wanna catch exceptions that could occur here
return "Webservice says OK";
}
精彩评论