转载自:http://huangqiqing123.iteye.com/blog/1454819
测试环境:axis2-1.6.1、6.0.20、jdk1.5
说明:本方式仅适用于文件小于10M的场景(否则会出现内存溢出),大文件的上传下载应另选其他方式。
?
1、创建要发布成webservice的java类。
- import?java.io.FileInputStream;??
- import?java.io.FileOutputStream;??
- import?java.io.IOException;??
- ??
- public?class?BlobService?{??
- ??
- ?????
- ?
- ??
- ????public?boolean?uploadFile(String?fileName,byte[]?bytes)??
- ????{??
- ????????FileOutputStream?fos?=?null;??
- ????????try{??
- ????????????fos?=?new?FileOutputStream("F:"+fileName);??
- ??????????????
- ??????????????
- ????????????fos.write(bytes);??
- ????????????fos.flush();??
- ????????}catch?(Exception?e){??
- ????????????e.printStackTrace();??
- ????????????return?false;??
- ????????}finally{??
- ????????????try?{??
- ????????????????fos.close();??
- ????????????}?catch?(IOException?e)?{??
- ????????????????e.printStackTrace();??
- ????????????}?????
- ????????}??
- ????????return?true;??
- ????}??
- ?????
- ?
- ??
- ????public?byte[]?downloadFile()??
- ????{??
- ????????String?filepath?=?"F:head.jpg";??
- ????????FileInputStream?in?=?null;??
- ????????byte?bytes[]?=?null;??
- ????????try?{??
- ????????????in?=?new?FileInputStream(filepath);??
- ????????????bytes?=?new?byte[in.available()];??
- ??????????????
- ??????????????
- ????????????in.read(bytes);??
- ????????}?catch?(Exception?e)?{??
- ????????????e.printStackTrace();??
- ????????}finally{?????????
- ????????????try?{??
- ????????????????in.close();??
- ????????????}?catch?(IOException?e)?{??
- ????????????????e.printStackTrace();??
- ????????????}??
- ????????}??
- ????????return?bytes;??
- ????}??
- }??
?
?
2、将上面的java类编译后的class文件放到axis2WEB-INFpojo目录下。
?
3、编写客户端程序。
- package?client;??
- ??
- import?java.io.FileInputStream;??
- import?java.io.FileOutputStream;??
- import?java.util.Date;??
- import?javax.xml.namespace.QName;??
- import?org.apache.axis2.addressing.EndpointReference;??
- import?org.apache.axis2.client.Options;??
- import?org.apache.axis2.rpc.client.RPCServiceClient;??
- ??
- public?class?BlobRPCClient??
- {??
- ????public?static?void?main(String[]?args)?throws?Exception??
- ????{??
- ????????RPCServiceClient?serviceClient?=?new?RPCServiceClient();??
- ????????Options?options?=?serviceClient.getOptions();??
- ????????EndpointReference?targetEPR?=?new?EndpointReference("http://localhost:8080/axis2/services/BlobService");??
- ????????options.setTo(targetEPR);??
- ?????????
- ??????????
- ??????????
- ????????String?filePath?=?"f:head.jpg";??
- ????????FileInputStream?fis?=?new?FileInputStream(filePath);??
- ?????????
- ??????????
- ????????byte[]?buffer?=?new?byte[fis.available()];??
- ??????????
- ??????????
- ????????fis.read(buffer);????
- ????????
- ??????????
- ????????Object[]?opAddEntryArgs?=?new?Object[]{"我是上传的文件.jpg",?buffer};??
- ??????????
- ??????????
- ????????Class<?>[]?classes?=?new?Class<?>[]{?Boolean.class?};??
- ??????????
- ??????????
- ????????QName?opAddEntry?=?new?QName("http://ws.apache.org/axis2","uploadFile");??
- ??????????
- ??????????
- ????????fis.close();??
- ???????
- ??????????
- ????????System.out.println(new?Date()+"?文件上传开始");??
- ????????Object?returnValue?=?serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,?classes)[0];??
- ????????System.out.println(new?Date()+"?文件上传结束,返回值="+returnValue);??
- ????????
- ??????????
- ??
- ????????opAddEntry?=?new?QName("http://ws.apache.org/axis2",?"downloadFile");??
- ??????????
- ????????System.out.println(new?Date()+"?文件下载开始");??
- ????????byte?bytes[]?=?(byte[])?serviceClient.invokeBlocking(opAddEntry,?new?Object[]{},?new?Class[]{byte[].class})[0];??
- ????????FileOutputStream?fileOutPutStream?=?new?FileOutputStream("F:我是下载的文件.jpg");??
- ?????????
- ??????????
- ????????fileOutPutStream.write(bytes);??
- ????????fileOutPutStream.flush();??
- ????????fileOutPutStream.close();??
- ????????System.out.println(new?Date()+"?文件下载完成");??
- ????}??
- }??
?
?
4、运行客户端程序,输出结果如下:
- Thu?Mar?15?20:42:55?CST?2012?文件上传开始??
- Thu?Mar?15?20:42:56?CST?2012?文件上传结束,返回值=true??
- Thu?Mar?15?20:42:56?CST?2012?文件下载开始??
- Thu?Mar?15?20:42:56?CST?2012?文件下载完成??
?
?
5、打开目录 F:,会看到:

?
http://huangqiqing123.iteye.com/admin/blogs/1454819