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

[b]配置Mule和XFire环境[/b]
参考前面的文章[url=http://hideto.iteye.com/blog/65607]实战Mule:利用Mule调用XFire发布的Web服务[/url]。

[b]利用XFire发布一个文件上传Web服务UploadService[/b]
在Eclipse里新建项目webservice,目录结构如下:
[code]
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
[/code]
其中services.xml如下:
[code]
<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>
[/code]
让我们看看UploadService.java:
[code]
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!";
}

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

[b]利用Mule构建我们的ESB中心[/b]
在Eclipse里创建新项目esb,目录结构如下:
[code]
esb
web
WEB-INF
lib
mule-services-config.xml
web.xml
index.jsp
[/code]
其中mule-services-config.xml配置了我们的UploadService:
[code]
<?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>
[/code]
看看我们怎么在前台index.jsp页面上传文件数据:
[code]
<%@ 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>
[/code]
注意这里我们用到了commons-fileupload组件库,其他库采用上一篇[url=http://hideto.iteye.com/blog/65607]实战Mule:利用Mule调用XFire发布的Web服务[/url]的库。
我们用org.codehaus.xfire.util.Base64工具类把上传文件的byte数组encode为String,并作为参数传递给Web服务。

[b]测试及源代码[/b]
按照上篇文章[url=http://hideto.iteye.com/blog/65607]实战Mule:利用Mule调用XFire发布的Web服务[/url]的讲述来启动两个Tomcat测试,打开
浏览器访问[url]http://localhost:8080/esb[/url]并Browse一张jpg图片,然后点击提交,看看我们的"D:\"下是不是多了一张test.jpg?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值