8
I am developing a java client to consume the WCF service below. Since the WCF service is using the Stream tech, I also hope to use the stream tech in java client code to reduce the memory use. Is there anyone can provide me an advice about how to write it? Thanks.
namespace TmpWcfStream
{
[MessageContract]
public class InputMessage
{
[MessageHeader(MustUnderstand = true)]
public string TranName { get; set; }
[MessageBodyMember(Order=1)]
public Stream FileData { get; set; }
}
[MessageContract]
public class OutputMessage
{
[MessageBodyMember(Order = 100)]
public bool Success { get; set; }
[MessageBodyMember(Order = 200)]
public string ErrorMsg { get; set; }
}
}
namespace TmpWcfStream
{
public class UploadDataSvc : UploadSvcContract
{
private const int bufferLen = 4096;
public OutputMessage UploadData(InputMessage msg)
{
OutputMessage outputMsg = new OutputMessage { ErrorMsg = string.Empty, Success = true };
try
{
Stream inputStream = msg.FileData;
String filePath = @"C:\tmp\output.txt";
using (FileStream targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = inputStream.Read(buffer, 0, bufferLen)) > 0)
{
targetStream.Write(buffer, 0, count);
}
}
inputStream.Close();
}
catch(Exception e)
{
outputMsg.Success = false;
outputMsg.ErrorMsg = e.ToString();
}
return outputMsg;
}
}