webservice文件上传下载(byte[] 实现方式)
来源:互联网 发布:java工控软件开发 编辑:程序博客网 时间:2024/06/03 14:00
测试环境: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、运行客户端程序,输出结果如下
Thu Mar 15 20:42:55 CST 2012 文件上传开始Thu Mar 15 20:42:56 CST 2012 文件上传结束,返回值=trueThu Mar 15 20:42:56 CST 2012 文件下载开始Thu Mar 15 20:42:56 CST 2012 文件下载完成
5、打开目录 F:\,会看到
可能用到的jar包:
0 0
- webservice文件上传下载(byte[] 实现方式)
- webservice文件上传下载(byte[] 实现方式)
- webservice实现文件上传下载(小文件)
- WebService中实现 上传下载文件
- WebService中实现 上传下载文件
- CXF-webService实现文件上传下载
- WebService 实现文件的上传下载(非自动生成)
- 文件上传下载webservice
- webservice 上传下载 大文件
- webservice上传下载文件3
- iOS开发之结合asp.net webservice实现文件上传下载(转)
- iOS开发之结合asp.net webservice实现文件上传下载
- iOS开发之结合asp.net webservice实现文件上传下载
- 再次奉献源码,webservice实现的多线程断点文件上传下载
- iOS开发之结合asp.net webservice实现文件上传下载
- iOS开发之结合asp.net webservice实现文件上传下载
- iOS开发之结合asp.net webservice实现文件上传下载
- iOS开发之结合asp.net webservice实现文件上传下载
- IAR下uCOS调试任务name为"?"
- 6. 面对对象(高级) Part 2 --- 学习笔记
- leetcode: Permutations
- iOS运行时获取对象的成员变量和成员方法
- MyEclipse 修改注释模版
- webservice文件上传下载(byte[] 实现方式)
- 心是净土,也是红尘
- maven在对resources目录下的文件进行过滤时出现部分文件未能正确替换
- CSS A link hover active visited伪类超链接锚文本样式教程
- several technical limits to the NameNode
- windows phone 8 开发学习笔记(三)
- android developer镜像网站
- 实战总结经验分享,同磁盘下安装多linux添加启动项到grub
- likely(x)