通过WSE-Attachment传输文件,参数有中文的问题?

通过WSE-Attachment传输文件确实效率很高,但我在开发过程中也遇到了一个很奇观的问题。当通过WSE上传文件时,如果Web Services方法的参数中包含有中文,则会出现乱码;然而通过WSE下载文件时却不会出现乱码,代码如下:

服务端代码:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using Microsoft.Web.Services;
using Microsoft.Web.Services.Dime;

namespace ImageService
{
/// <summary>
/// Summary description for Service1.
/// </summary>
[WebService(Namespace="http://demo.miti.bbi.edu.cn/dime/",Description="Web service returns one or more JPEG files using DIME.")]
public class ImageService : System.Web.Services.WebService
{
public ImageService()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}

#region Component Designer generated code

//Required by the Web Services Designer
private IContainer components = null;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

// WEB SERVICE EXAMPLE
// The HelloWorld() example service returns the string Hello World
// To build, uncomment the following lines then save and build the project
// To test this web service, press F5

// [WebMethod]
// public string HelloWorld()
// {
// return "Hello World";
// }

[WebMethod]
public string [] GetImage(string[] imageNameCollection)
{
// Get the SoapContext for the response message
SoapContext responseContext = HttpSoapContext.ResponseContext;

// Create an array that returns URIs for the related DIME attachments.
string[] retUri= new string[imageNameCollection.Length];

int i = 0; // Iterrator

// Create a DimeAttachment object for each file specified
// by the values of the imageID array.
foreach (string imageName in imageNameCollection)
{
// String that represents the file name and path for the attachment.
string filePath = "C:\\WSE\\Get\\SourceImages\\" + imageName + ".doc";

// Create a new DIME attachment using the the file name and
// specifying the encoding of the attachment using the MIME media
// type of image\jpeg.
DimeAttachment dimeImage = new DimeAttachment("text/richtext", TypeFormatEnum.MediaType,filePath);

// Generate a GUID-based URI reference for the attachment object
// and assign in to the ID property of the DIME record.

dimeImage.Id = "uri:" + Guid.NewGuid().ToString();

// Add the new DimeAttachment object to the SoapContext object.
responseContext.Attachments.Add(dimeImage);

// Add the generated URI to array that is returned.
retUri[i] = dimeImage.Id;

i++;
}

// Return the array of URIs that match the ID vaules of
// the attachments.
return retUri;
}

[WebMethod]
public void SendImage(string[] imageNameCollection)
{
// Get the SoapContext for the Request message
SoapContext requestContext = HttpSoapContext.RequestContext;

// Check if request message contains any attachments.
if (requestContext.Attachments.Count > 0)
{
// Display each attached BMP image file.
for (int i = 0; i < requestContext.Attachments.Count;i ++)
{
// Stream the attached image into a new Bitmap object to display.
Bitmap image = new Bitmap(requestContext.Attachments[i].Stream);

// Do something with the image.
image.Save("C:\\WSE\\Send\\DestinationImage\\" + imageNameCollection[i] + ".bmp",ImageFormat.Bmp);
}
}
}

}


客户端代码:


using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Web.Services;
using Microsoft.Web.Services.Dime;

namespace ImageClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnGetImage;
private System.Windows.Forms.Button btnSendImage;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

const int SizeBuff = 1024;

public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnGetImage = new System.Windows.Forms.Button();
this.btnSendImage = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnGetImage
//
this.btnGetImage.Location = new System.Drawing.Point(40, 88);
this.btnGetImage.Name = "btnGetImage";
this.btnGetImage.TabIndex = 0;
this.btnGetImage.Text = "GetImage";
this.btnGetImage.Click += new System.EventHandler(this.btnGetImage_Click);
//
// btnSendImage
//
this.btnSendImage.Location = new System.Drawing.Point(168, 88);
this.btnSendImage.Name = "btnSendImage";
this.btnSendImage.TabIndex = 1;
this.btnSendImage.Text = "SendImage";
this.btnSendImage.Click += new System.EventHandler(this.btnSendImage_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 165);
this.Controls.Add(this.btnSendImage);
this.Controls.Add(this.btnGetImage);
this.Name = "frmMain";
this.Text = "MainForm";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}

private void btnGetImage_Click(object sender, System.EventArgs e)
{
string[] imageNameCollection = new string[2];
ImageService.ImageServiceWse imageService = new ImageService.ImageServiceWse();

imageNameCollection[0] = "文档0";
imageNameCollection[1] = "文档1";


try
{
// Call the Web method passing an array of image names, and
// capture the URI values in the returned array in case we
// need to reference the attachments by ID.
string[] retUri = imageService.GetImage(imageNameCollection);
}
catch (Exception ex)
{
// Handle any exception errors.;
MessageBox.Show(ex.Message);

return;
}

// Check if response message contains any attachments.
if (imageService.ResponseSoapContext.Attachments.Count > 0)
{
// Display each attached BMP image file.
for (int i = 0; i < imageService.ResponseSoapContext.Attachments.Count;i ++)
{
// // Stream the attached image into a new Bitmap object to display.
// Bitmap image = new Bitmap(imageService.ResponseSoapContext.Attachments[i].Stream);
//
// // Do something with the image.
// image.Save("C:\\WSE\\Get\\DestinationImage\\" + imageNameCollection[i] + ".bmp",ImageFormat.Bmp);

// create binary streams
Stream outputStream = File.OpenWrite("C:\\WSE\\Get\\DestinationImage\\" + imageNameCollection[i] + ".doc");

// add buffered streams on top of the binary streams
BufferedStream bufferedOutput = new BufferedStream(outputStream);

Byte[] buffer = new Byte[SizeBuff];

int bytesRead;
while((bytesRead = imageService.ResponseSoapContext.Attachments[i].Stream.Read(buffer,0,SizeBuff)) > 0 )
{
bufferedOutput.Write(buffer,0,bytesRead);
}

bufferedOutput.Flush( );
bufferedOutput.Close( );

}

MessageBox.Show(imageService.ResponseSoapContext.Attachments.Count + " images have recieved!");
}
else
{
// Warn the user if no images were returned.
MessageBox.Show("No images were returned");
}
}

private void btnSendImage_Click(object sender, System.EventArgs e)
{
string[] imageNameCollection = new string[2];
ImageService.ImageServiceWse imageService = new ImageService.ImageServiceWse();

imageNameCollection[0] = "图片0";
imageNameCollection[1] = "图片1";

try
{
// Create an array that returns URIs for the related DIME attachments.
string[] retUri= new string[imageNameCollection.Length];

int i = 0; // Iterrator

// Create a DimeAttachment object for each file specified
// by the values of the imageID array.
foreach (string imageName in imageNameCollection)
{
// String that represents the file name and path for the attachment.
string filePath = "C:\\WSE\\Send\\SourceImages\\" + imageName + ".bmp";

// Create a new DIME attachment using the the file name and
// specifying the encoding of the attachment using the MIME media
// type of image\jpeg.
DimeAttachment dimeImage = new DimeAttachment("image/bmp", TypeFormatEnum.MediaType,filePath);

// Generate a GUID-based URI reference for the attachment object
// and assign in to the ID property of the DIME record.

dimeImage.Id = "uri:" + Guid.NewGuid().ToString();

// Add the new DimeAttachment object to the SoapContext object.
imageService.RequestSoapContext.Attachments.Add(dimeImage);

// Add the generated URI to array that is returned.
retUri[i] = dimeImage.Id;

i ++;
}

// Call the Web method passing an array of image names, and
// capture the URI values in the returned array in case we
// need to reference the attachments by ID.
imageService.SendImage(imageNameCollection);

MessageBox.Show(i.ToString() + " images have sent!");

}
catch (Exception ex)
{
// Handle any exception errors.;
MessageBox.Show(ex.Message);
}
}
}
}

请大家多多指教!
posted @ 2006-08-09 12:31  DotNet菜园  阅读(986)  评论(0)    收藏  举报