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的文件。
OSGI中,以Web Service的方式提供文件上传的接口
最新推荐文章于 2024-02-06 09:51:30 发布