Axis 1.4 上传二进制文件(base64Binary)

服务端代码:

Java代码
package samples.userguide.example5;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class BinaryServer {

public void saveImage(String filename, byte[] image) throws IOException {
String dir = "D:/workspace-java/axis1/web/WEB-INF/classes/samples/userguide/example5/";
if (image != null && image.length > 0) {
FileOutputStream output = new FileOutputStream(new File(dir
+ filename));
output.write(image);
output.flush();
output.close();
}
}
}

package samples.userguide.example5;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class BinaryServer {

public void saveImage(String filename, byte[] image) throws IOException {
String dir = "D:/workspace-java/axis1/web/WEB-INF/classes/samples/userguide/example5/";
if (image != null && image.length > 0) {
FileOutputStream output = new FileOutputStream(new File(dir
+ filename));
output.write(image);
output.flush();
output.close();
}
}
} 配置deploy.wsdl文件,由于byte[]是axis默认支持的,因此不需要额外的配置。

Xml代码
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="BinaryServer" provider="java:RPC">
<parameter name="className" value="samples.userguide.example5.BinaryServer"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<service name="BinaryServer" provider="java:RPC">
<parameter name="className" value="samples.userguide.example5.BinaryServer"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>运行发布描述文件命令:

Cmd代码
D:\workspace-java\axis\web\WEB-INF>java -classpath %CLASSPATH% org.apache.axis.client.AdminClient -l http://localhost:8080/axis/services/BinaryServer deploy.wsdd

D:\workspace-java\axis\web\WEB-INF>java -classpath %CLASSPATH% org.apache.axis.client.AdminClient -l http://localhost:8080/axis/services/BinaryServer deploy.wsdd
客户端代码:

Java代码
package samples.userguide.example5;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;

public class BinaryClient {

public static void main(String[] args) throws ServiceException, IOException {
String url = "http://localhost:8080/axis/services/BinaryServer";
String dir = BinaryClient.class.getResource(".").getFile().toLowerCase();
System.out.println(dir);
// System.exit(0);
FileInputStream input = new FileInputStream(new File(dir+"Desert.jpg"));
byte [] image = new byte[input.available()];
input.read(image);
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
org.apache.axis.client.Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
javax.xml.namespace.QName qn = new javax.xml.namespace.QName("BinaryServer","saveImage");
call.setOperationName(qn);
call.addParameter("arg0", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.addParameter("arg1", org.apache.axis.encoding.XMLType.SOAP_BASE64BINARY, javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.AXIS_VOID);
Object[] args1 = new Object[]{"desert1.jpg",image};
call.invoke(args1);
input.close();
}
}

package samples.userguide.example5;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;

public class BinaryClient {

public static void main(String[] args) throws ServiceException, IOException {
String url = "http://localhost:8080/axis/services/BinaryServer";
String dir = BinaryClient.class.getResource(".").getFile().toLowerCase();
System.out.println(dir);
// System.exit(0);
FileInputStream input = new FileInputStream(new File(dir+"Desert.jpg"));
byte [] image = new byte[input.available()];
input.read(image);
org.apache.axis.client.Service service = new org.apache.axis.client.Service();
org.apache.axis.client.Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
javax.xml.namespace.QName qn = new javax.xml.namespace.QName("BinaryServer","saveImage");
call.setOperationName(qn);
call.addParameter("arg0", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
call.addParameter("arg1", org.apache.axis.encoding.XMLType.SOAP_BASE64BINARY, javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.encoding.XMLType.AXIS_VOID);
Object[] args1 = new Object[]{"desert1.jpg",image};
call.invoke(args1);
input.close();
}
} 运行测试,OK。确实上传成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值