应用AXIS2框架向服务端上传文件:
服务端代码:
public void uploadImageWithByte(byte[] imageByte, int length){
FileOutputStream fos = null;
try
{
// 将上传的图像保存在D盘的test1.jpg文件中
fos = new FileOutputStream("c:/test/test1.DAT");
// 开始写入图像文件的字节
fos.write(imageByte, 0, length);
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (fos != null)
{
try
{
fos.close();
}
catch (Exception e)
{
}
}
}
}
客户端代码:
RPCServiceClient serviceClient;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(ClientCommonParameter.NotFoundsUrl);
options.setTo(targetEPR);
// 下面的代码调用uploadImageWithByte方法上传图像文件
/
// 打开图像文件,确定图像文件的大小
java.io.File file = new java.io.File("c:/Upload/test1.DAT");
java.io.FileInputStream fis = null;
fis = new java.io.FileInputStream("c:/Upload/test1.DAT");
// 创建保存要上传的图像文件内容的字节数组
byte[] buffer = new byte[(int) file.length()];
// 将图像文件的内容读取buffer数组中
int n = 0;
n = fis.read(buffer);
//n为读入的字节数
System.out.println("文件长度:" + file.length());
Object[] opAddEntryArgs = new Object[]{ buffer, n };
Class[] classes = new Class[]{ };
QName opAddEntry = new QName(ClientCommonParameter.Qurl,"uploadImageWithByte");
fis.close();
serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes);
// 开始上传图像文件,并输出uploadImageWithByte方法的返回传
// System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
======================================================================
文件下载客户端:
RPCServiceClient serviceClient;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(ClientCommonParameter.NotFoundsUrl);
options.setTo(targetEPR);
Object[] opAddEntryArgs = new Object[] {};
Class[] classes = new Class[] {byte[].class};
QName opAddEntry = new QName(ClientCommonParameter.Qurl, "down");
byte[] strArray = (byte[])serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
FileOutputStream fos = null;
// 将下载的图像保存在D盘的test1.jpg文件中
fos = new FileOutputStream("c:/test/bb.DAT");
// 开始写入图像文件的字节
fos.write(strArray);
fos.close();
System.out.println("OK");
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
服务端:
public byte[] down() throws Exception
{
// 下面的代码调用uploadImageWithByte方法上传图像文件
// 打开图像文件,确定图像文件的大小
java.io.File file = new java.io.File("c:/test/test1.DAT");
java.io.FileInputStream fis = new java.io.FileInputStream
("c:/test/test1.DAT");
// 创建保存要上传的图像文件内容的字节数组
byte[] buffer = new byte[(int) file.length()];
// 将图像文件的内容读取buffer数组中
int n = fis.read(buffer); //n为读入的字节数
System.out.println("文件长度:" + file.length());
fis.close();
return buffer;
}