备忘录

记录学习过、研究过、使用过和总结过的内容,以备不时之需

统计

留言簿(4)

积分与排名

其它

技术网站

牛人博客

阅读排行榜

评论排行榜

实战Mule:利用Mule调用XFire发布的文件上传服务(转)

配置Mule和XFire环境
参考前面的文章实战Mule:利用Mule调用XFire发布的Web服务

利用XFire发布一个文件上传Web服务UploadService
在Eclipse里新建项目webservice,目录结构如下:
webservice
src-service
cn.hidetoishandsome.xfire.service
IUploadService.java
cn.hidetoishandsome.xfire.service.impl
UploadService.java
src-conf
META-INF
xfire
services.xml
web
WEB-INF
lib
web.xml

其中services.xml如下:
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>UploadService</name>
<namespace>http://localhost:9090/webservice/services/UploadService</namespace>
<serviceClass>cn.hidetoishandsome.xfire.service.IUploadService</serviceClass>
<implementationClass>cn.hidetoishandsome.xfire.service.impl.UploadService</implementationClass>
</service>
</beans>

让我们看看UploadService.java:
package cn.hidetoishandsome.xfire.service.impl;
import java.io.File;
import java.io.FileOutputStream;
import org.codehaus.xfire.util.Base64;
import cn.hidetoishandsome.xfire.service.IUploadService;
public class UploadService implements IUploadService {
public String upload(String encodedFileString) {
FileOutputStream fos = null;
File file = new File("D:\\test.jpg");
byte[] bytes = Base64.decode(encodedFileString);
try {
fos = new FileOutputStream(file);
fos.write(bytes);
fos.flush();
fos.close();
} catch (Exception e) {
return "Wrong!";
}
return "Success!";
}
}

这里我们假设上传一张jpg图片,并将Base64binary encode为String,然后在UploadService里decode为byte[]并写入Disk。

利用Mule构建我们的ESB中心
在Eclipse里创建新项目esb,目录结构如下:
esb
web
WEB-INF
lib
mule-services-config.xml
web.xml
index.jsp

其中mule-services-config.xml配置了我们的UploadService:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mule-configuration PUBLIC "-//MuleSource //DTD mule-configuration XML V1.0//EN"
"http://mule.mulesource.org/dtds/mule-configuration.dtd">
<mule-configuration id="Mule_Demo" version="1.0">
<mule-descriptor name="UploadService" inboundEndpoint="vm://uploadservice" implementation="org.mule.components.simple.BridgeComponent">
<outbound-router>
<router className="org.mule.routing.outbound.OutboundPassThroughRouter">
<endpoint address="wsdl-xfire:http://localhost:9090/webservice/services/UploadService?wsdl&method=upload"/>
</router>
</outbound-router>
</mule-descriptor>
</mule-configuration>

看看我们怎么在前台index.jsp页面上传文件数据:
<%@ page import="org.mule.extras.client.MuleClient, org.mule.umo.UMOMessage, org.apache.commons.fileupload.disk.DiskFileItemFactory, org.apache.commons.fileupload.servlet.ServletFileUpload, org.apache.commons.fileupload.*, java.util.List, org.codehaus.xfire.util.Base64"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Mule Upload Example</title>
</head>
<body>
<%
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List fileItems = upload.parseRequest(request);
FileItem item = (FileItem) fileItems.get(0);
MuleClient client = new MuleClient();
byte[] bytes = item.get();
String encodedFileString = Base64.encode(bytes);
UMOMessage message = client.send("vm://uploadservice", encodedFileString, null);
out.print(message.getPayload());
} catch(Exception e) {
}
%>
<form method="POST" name="uploadFile" action="" enctype="multipart/form-data">
<table>
<tr><td>
<input type="file" name="file"/></td><td><input type="submit" name="Go" value=" Go " />
</td></tr>
</table>
</form>
<p/>
</body>
</html>

注意这里我们用到了commons-fileupload组件库,其他库采用上一篇实战Mule:利用Mule调用XFire发布的Web服务的库。
我们用org.codehaus.xfire.util.Base64工具类把上传文件的byte数组encode为String,并作为参数传递给Web服务。

测试及源代码
按照上篇文章实战Mule:利用Mule调用XFire发布的Web服务的讲述来启动两个Tomcat测试,打开
浏览器访问http://localhost:8080/esb并Browse一张jpg图片,然后点击提交,看看我们的"D:\"下是不是多了一张test.jpg?

posted on 2008-09-09 16:27 雪山飞狐 阅读(808) 评论(0)  编辑  收藏 所属分类: 开源框架


只有注册用户登录后才能发表评论。


网站导航: