在《类的代码如下:
package service;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
public class FileService
{
// 使用byte[]类型参数上传二进制文件
public boolean uploadWithByte(byte[] file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(filename);
fos.write(file);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
{
int n = 0;
byte[] buffer = new byte[8192];
while((n = is.read(buffer)) > 0)
{
os.write(buffer, 0, n);
}
}
// 使用DataHandler类型参数上传文件
public boolean uploadWithDataHandler(DataHandler file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(filename);
// 可通过DataHandler类的getInputStream方法读取上传数据
writeInputStreamToFile(file.getInputStream(), fos);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
}
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import javax.activation.DataHandler;
public class FileService
{
// 使用byte[]类型参数上传二进制文件
public boolean uploadWithByte(byte[] file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(filename);
fos.write(file);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
private void writeInputStreamToFile(InputStream is, OutputStream os) throws Exception
{
int n = 0;
byte[] buffer = new byte[8192];
while((n = is.read(buffer)) > 0)
{
os.write(buffer, 0, n);
}
}
// 使用DataHandler类型参数上传文件
public boolean uploadWithDataHandler(DataHandler file, String filename)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(filename);
// 可通过DataHandler类的getInputStream方法读取上传数据
writeInputStreamToFile(file.getInputStream(), fos);
fos.close();
}
catch (Exception e)
{
return false;
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
return true;
}
}
上面代码在services.xml文件的配置代码如下:
<service name="fileService">
<description>
文件服务
</description>
<parameter name="ServiceClass">
service.FileService
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
<description>
文件服务
</description>
<parameter name="ServiceClass">
service.FileService
</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
</service>
如果使用wsdl2java命令生成调用Java客户端代码,则需要创建DataHandler类的对象实例,代码如下:
DataHandler dh = new DataHandler(new FileDataSource(imagePath));
wsdl2java命令会为每一个方法生成一个封装方法参数的类,类名为方法名(第一个字符大写),如uploadWithByte方法生成的类名为UploadWithByte。如果要设置file参数的值,可以使用UploadWithByte类的setFile方法,代码如下:
UploadWithByte uwb = new UPloadWithByte();
uwb.setFile(dh);
uwb.setFile(dh);
最后是调用uploadWithByte方法,代码如下(FileServiceStub为wsdl2java生成的stub类名):
FileServiceStub fss = new FileServiceStub();
fss.uploadWithByte(uwb);
fss.uploadWithByte(uwb);
如果使用C#调用FileService,则file参数类型均为byte[],代码如下:
MemoryStream ms = new MemoryStream();
Bitmap bitmap = new Bitmap(picUpdateImage.Image);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
service.fileService fs = new WSC.service.fileService();
fs.uploadWithDataHandler(ms.ToArray());
fs.uploadWithByte(ms.ToArray());
Bitmap bitmap = new Bitmap(picUpdateImage.Image);
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
service.fileService fs = new WSC.service.fileService();
fs.uploadWithDataHandler(ms.ToArray());
fs.uploadWithByte(ms.ToArray());
其中picUpdateImage为c#中加载图像文件的picturebox控件。