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

服务端代码:

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默认支持的,因此不需要额外的配置。


    
        
        
    

运行发布描述文件命令:

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

客户端代码:

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。确实上传成功。

 

你可能感兴趣的:(Java/XML)