OSGI中,以Web Service的方式提供文件上传的接口

OSGI中,以Web Service的方式提供文件上传的接口

	/**
	 * TODO 本系统提供的文件上传接口<br>
	 * 		
	 */
	@OSGiService(interfaces = { IUpLoadService.class }, properties = {
			"service.exported.interfaces=*",
			"org.apache.cxf.ws.httpservice.context=/services/jxnTest/UpLoadService" })
	@Component
	public class UpLoadService implements IUpLoadService{
		
		public String upLoadFile(final byte[] fileByteArray) {
			//创建一个保存文件的输出流
			File file = new File("D:\\其它系统传过来的文件.zip");
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(file);
				fos.write(fileByteArray);
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			return "beautiful";
		}	
	}

	说明:
		1)通过@OSGiService注解来发布注册web服务。
			"org.apache.cxf.ws.httpservice.context=/services/jxnTest/UpLoadService" 指定wsdl文件的地址。
		2)控制台打印信息:Successfully registered CXF DOSGi servlet at /services/jxnTest/UpLoadService 表明web服务注册发布成功。
		3)接口方法中传递的参数类型必须是可序列化的。

	-----------------------------------------------------------------

	/**
	 * TODO 其它系统调用这个文件上传接口的客户端<br>
	 * @author Administrator <br> 
	 */
	public class UploadClient {

		public static void main(final String[] args) throws Exception {
			
			File file = new File("D:\\要上传的文件.zip");
			FileInputStream fis = new FileInputStream(file); 
			
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);  
			
			byte[] buffer = new byte[1024];
			while (fis.read(buffer) != -1) {  
				bos.write(buffer);  
			} 
			fis.close();  
			bos.close();  
			
			// 使用 ByteArrayOutputStream 创建一个字节数组
			byte[] fileBuffer = bos.toByteArray();
			
			// 调用其它系统提供的webService接口
			// 	注意:在eclipse中,可以通过 新建-其他-Web Services-Web Service Client,然后输入wsdl文件的地址,即可生成客户端调用代码;
			// 		-->	类IUpLoadServicePortTypeProxy就是通过上面这种方法生成的。
			IUpLoadServicePortTypeProxy proxy = new IUpLoadServicePortTypeProxy();
			proxy.upLoadFile(fileBuffer);
			
			System.out.println("client finish!");
		}
	}



	环境:
		jdk1.6
		
	说明:
		1)测试结果:JVM参数设为-Xmx1024m -Xms512m 时,通过字节数组的方式最大可以上传85M的文件。
	
	
	
	
		

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值