接口 interface
import
?J2EE技術的爬蟲.util.Collection;
import
?J2EE技術的爬蟲.util.List;


public
?
interface
?IMathService?
{

?
/**?*/
/**
??*?加
??*?
@param
?a
??*?
@param
?b
??*?
@return
??
*/
?
public
?
int
?add(
int
?a,
int
?b);

?
/**?*/
/**
??*?減
??*?
@param
?a
??*?
@param
?b
??*?
@return
??
*/
?
public
?
int
?sub(
int
?a,
int
?b);

/**?*/
/**
上傳二進制文件
*/
??
public
?String?sendFile(String?fileName,
byte
[]?file?);
?
}
實現 implements
import
?J2EE技術的爬蟲.io.File;
import
?J2EE技術的爬蟲.io.FileOutputStream;
import
?J2EE技術的爬蟲.sql.Connection;
import
?J2EE技術的爬蟲.sql.ResultSet;
import
?J2EE技術的爬蟲.sql.Statement;
import
?J2EE技術的爬蟲.util.ArrayList;
import
?J2EE技術的爬蟲.util.Collection;
import
?J2EE技術的爬蟲.util.List;

import
?com.newsoft.oa.bean.User;
import
?com.newsoft.oa.uitl.Connector;
import
?com.thoughtworks.xstream.XStream;


public
?
class
?MathServiceImpl?
implements
?IMathService
{

??
public
?
int
?add(
int
?a,
int
?b)
{
???
return
?a
+
b;
??}
??

??
public
?
int
?sub(
int
?a,
int
?b)
{
???
return
?a
-
b;
??}
?

??
public
?String?getWelComeStr(String?name)
{
???
return
?
"
hi?
"
+
name
+
"
!?歡迎你
"
;
??}
??
public
?List?getUsers()
{
???List?l
=
new
?ArrayList();
???l.add(
"
name
"
);
???l.add(
"
password
"
);
???l.add(
"
sex
"
);
???
return
?l;
??}
public
?String?sendFile(String?fileName,?
byte
[]?filebytes)?
{

?
try
{
??String?path
=
""
;

???
if
(filebytes
!=
null
&&
filebytes.length
>
0
)
{
????File?file
=
new
?File(
"
/
"
+
fileName);
????
????file.createNewFile();
????FileOutputStream?fos
=
new
?FileOutputStream(file);
????fos.write(filebytes);
????fos.close();
????path
=
file.getAbsolutePath();
????System.out.println(path);
????
????file
=
null
;
????
???}
???
???
return
?path;

?}
catch
(Exception?ex)
{
??
return
?
"
false
"
;
?}
}
}
?
配置文件
放在 Classes/META-INF/xfire/service.xml;里面
<?
xml?version="1.0"?encoding="UTF-8"
?>
<
beans?
xmlns
="http://xfire.codehaus.org/config/1.0"
>
?
<
service
>
??
<
name
>
MathService
</
name
>
??
<
namespace
>
newsoft/oa/MathService
</
namespace
>
??
<
serviceClass
>
???com.newsoft.oa.services.IMathService
??
</
serviceClass
>
??
<
implementationClass
>
???com.newsoft.oa.services.MathServiceImpl
??
</
implementationClass
>
?
</
service
>
</
beans
>
其實是借鑒了Spring的寫法,用過Spring不會對著陌生,(Application-context.xml)
WEB-XML加上
<
servlet
>
??
<
servlet-name
>
XFireServlet
</
servlet-name
>
??
<
servlet-class
>
???org.codehaus.xfire.transport.http.XFireConfigurableServlet
??
</
servlet-class
>
?
</
servlet
>
?
<
servlet-mapping
>
??
<
servlet-name
>
XFireServlet
</
servlet-name
>
??
<
url-pattern
>
/servlet/XFireServlet/*
</
url-pattern
>
?
</
servlet-mapping
>
?
<
servlet-mapping
>
??
<
servlet-name
>
XFireServlet
</
servlet-name
>
??
<
url-pattern
>
/services/*
</
url-pattern
>
?
</
servlet-mapping
>
最后就是客戶端了
/?/Create?a?metadata?of?the?service??????????????
?Service?serviceModel?=?new?ObjectServiceFactory().create(IMathService.class);?
//?Create?a?proxy?for?the?deployed?service??????
??
?XFireProxyFactory?factory?=?new?XFireProxyFactory(XFireFactory.newInstance().getXFire());???
?String?serviceUrl?=?"http://localhost:8080/ws/services/MathService";
??client?=?null;???????

?try?
{??????????
???client?=?(IMathService)?factory.create(serviceModel,?serviceUrl);??
???File?file=new?File("c:\\SUPERMAP?白皮書.pdf");
???FileInputStream?fis=new?FileInputStream(file);
???byte[]?b=new?byte[fis.available()];
???fis.read(b);
??System.out.println(client.sendFile(file.getName(),?b));

?}?catch?(Exception?ex)?
{?????????
???ex.printStackTrace();
?}???????????????????????????//Invoke?the?service????
?int?serviceResponse?=?0;
?int?a=10,b=20;

就是
Service serviceModel = new ObjectServiceFactory().create(IMathService.class);?
?XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());??
?String serviceUrl = http://localhost:8080/ws/services/MathService;
三行字建立連接請求,
太輕松了
傳輸文件速度也可以,二進制,2M多的文件,也能輕松傳遞
用.net和delphi平臺測試,兼容性沒問題(按道理 soap,也不應該有問題)
這是為客戶搭建的在 檔案系統和OA審批間作文件歸檔的嘗試項目
哈,完整的項目代碼,就不方便講了。