转载自: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);
- //将字节数组bytes中的数据,写入文件输出流fos中
- 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中,将 bytes.length 个字节的数据读入字节数组bytes中
- in.read(bytes);
- } catch (Exception e) {
- e.printstacktrace();
- }finally{
- try {
- in.close();
- } catch (IOException e) {
- e.printstacktrace();
- }
- }
- return bytes;
- }
- }
2、将上面的java类编译后的class文件放到axis2\WEB-INF\pojo目录下。
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中的数据读入字节数组buffer中
- fis.read(buffer);
- //设置入参(1、文件名,2、文件字节流数组)
- Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", buffer};
- //返回值类型
- Class<?>[] classes = new Class<?>[]{ Boolean.class };
- //指定要调用的方法名及WSDL文件的命名空间
- 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");
- //将字节数组bytes中的数据,全部写入输出流fileOutPutStream中
- fileOutPutStream.write(bytes);
- fileOutPutStream.flush();
- fileOutPutStream.close();
- System.out.println(new Date()+" 文件下载完成");
- }
- }
4、运行客户端程序,输出结果如下:
5、打开目录 F:\,会看到:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。