(转).Net/C# 与 J2EE/Java Web Service 互操作完整实例
.Net 与 J2EE/Java Web Service 互操作完整实例
实现支持文件分块多点异步上传的 J2EE/Java Web Services 及其 .Net 非 Web 客户端应用程序异步上传
本文参考:
实现支持文件分块多点异步上传的 Web Services 及其客户端(非Web)应用程序调用相关异步执行的 Web Method
http://blog.csdn.net/playyuer/archive/2004/12/11/213069.aspx
升级到 JDK 5.0 Update 1 or 2 定制部署 WebServices - Axis 终于正常了!
http://blog.csdn.net/playyuer/archive/2005/03/01/306360.aspx
为了使程序及环境简洁,本文均使用最简陋的 NotePad 编写程序或配置环境,命令行编译程序!
不使用任何集成开发环境(IDE)!
相关源程序下载:
https://files.cnblogs.com/Microshaoft/jws.net.rar
Sever Side:
采用 Resin/Tomcat + Axis 来部署 java Web Service。
1.首先确认 Windows 系统中安装(不一定要安装,解压到目录即可)了如下软件:
J2SE(TM) Development Kit 5.0 Update 4 (JDK 5.0 Update 4):
http://java.sun.com/j2se/1.5.0/download.jsp
Web Application Server: Resin/Tomcat 二者有其一即可:
Resin v2.1.16:
http://www.caucho.com/download/resin-2.1.16.zip
Tomcat v5.5.9:
http://apache.justdn.org/jakarta/tomcat-5/v5.5.9/bin/jakarta-tomcat-5.5.9.zip
下载后解压到某目录,如:
D:\dotNet.J2EE\resin-2.1.16\
或
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\
Web Services - Axis
http://apache.freelamp.com/ws/axis/1_2_1/axis-bin-1_2_1.zip
下载后解压到某目录,如:
D:\dotNet.J2EE\axis-1_2_1\
然后将该目录下的 webapps 下的 axis 子目录,复制到:
D:\dotNet.J2EE\resin-2.1.16\webapps\
或
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\webapps\
即生成如下路径:
D:\dotNet.J2EE\resin-2.1.16\webapps\axis\
或
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\webapps\axis\
至此就具备运行 Axis 的基本条件了:
先做一下 Java Web Application Server 端口配置到 1080:
(如果你的端口不冲突,可以跳过此步)
Resin:
D:\Resin\resin-2.1.16\conf\resin.conf 用 IE 访问:
http://localhost:1080/axis/
应该看到:
Apache-AXIS 主页
点击超链接:
Validation - Validate the local installation's configuration
see below if this does not work.
访问页面:
http://localhost:1080/axis/happyaxis.jsp
就可以检测当前环境是否满足运行 Apache-AXIS。
根据页面提示下载到所有 zip 文件:
http://192.18.97.238/ECom/EComTicketServlet/BEGIN8656005BAB2BAA9A4B1688FB7D4AF765/-2147483648/972910143/1/359918/359906/972910143/2ts+/westCoastFSEND/7017-jaf-1.0.2-oth-JPR/7017-jaf-1.0.2-oth-JPR:1/jaf-1_0_2-upd2.zip
http://192.18.97.186/ECom/EComTicketServlet/BEGIN87ACAEFABC9A644DBEC89773E476BF24/-2147483648/972928407/1/540782/540770/972928407/2ts+/westCoastFSEND/javamail-1_3_2-oth-JPR/javamail-1_3_2-oth-JPR:1/javamail-1_3_2-upd.zip
http://xml.apache.org/security/dist/java-library/xml-security-bin-1_2_1.zip
各自解压后得到所需所有 jar 包文件:
activation.jar
axis-ant.jar
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
commons-logging-api.jar
commons-logging.jar
imap.jar
jaxrpc.jar
log4j-1.2.8.jar
mail.jar
mailapi.jar
pop3.jar
saaj.jar
smtp.jar
tt.txt
wsdl4j-1.5.1.jar
xalan.jar
xercesImpl.jar
xml-apis.jar
xmlsec-1.2.1.jar
xmlsecSamples-1.2.1.jar
xmlsecTests-1.2.1.jar
将这些文件全部复制到如下目录:
D:\dotNet.J2EE\resin-2.1.16\webapps\axis\WEB-INF\lib
或
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\webapps\axis\WEB-INF\lib
再次访问:
http://localhost:1080/axis/happyaxis.jsp
以验证是否完全满足运行 Apache-AXIS 的环境,
如无错误就说明已经配置好服务器了!
<http port='1080'/>
Tomcat:
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\conf
<Connector port="1080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
然后,启动 Java Web Application Server:
Resin:
D:\dotNet.J2EE\resin-2.1.16\bin\httpd.exe
Tomcat:
(启动 Tomcat 要预先配置 JAVA_HOME 环境变量)
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\bin\start.bat
2.编写 Java Web Service 程序:
其实实现 Web Service 的 Java 类没啥特殊的,
下面程序就是:
实现支持文件分块多点异步上传的 Java Web Services (Server Side)//=========================================================================
Class1.jws
Class1.java
javac Class1.java
java -cp %axis_lib%;%classpath%;. org.apache.axis.client.AdminClient deploy.wsdd -p1080
*/
import java.io.*;
import java.lang.*;
public class Class1
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
public String SayHelloTo(String Who)
{
return "你好: " + Who;
}
public String SayHelloToo(String Who)
{
return "你好: " + Who;
}
public String UploadFileBytes(byte[] Bytes,String FileName)
throws Exception
{
return UploadFileChunkBytes(Bytes, 0, FileName);
}
public String UploadFileChunkBytes(byte[] Bytes,int Position,String FileName)
throws Exception
{
//"d:\\Server\\Upload\\" 为服务器端路径
String ServerPath = "d:\\Server\\Upload\\";
java.io.RandomAccessFile raf = new java.io.RandomAccessFile(ServerPath + FileName,"rws");
raf.skipBytes(Position);
raf.write(Bytes);
//该 Bytes 的字节要写到 服务器端 相应文件的从 Position 开始的字节
raf.close();
raf = null;
System.gc();
return FileName + " 文件块: 位置[" + Position + "," + (Position + Bytes.length) + "] 大小(" + Bytes.length + ") 上传成功!";
}
public String CreateBlankFile(String FileName,int Length) //建议由客户端同步调用
throws Exception
{
//"d:\\Server\\Upload\\" 为服务器端路径
String ServerPath = "d:\\Server\\Upload\\";
FileOutputStream fos = new FileOutputStream(ServerPath + FileName);
fos.write(new byte[Length], 0, Length);
fos.close();
fos = null;
System.gc() ;
return FileName + " (" + Length + ") 空白文件已经创建!";
}
public byte[] DownloadFileBytes(String FileName)
throws Exception
{
File f = new File(FileName);
FileInputStream fis = new FileInputStream(f);
int i = (int) f.length();
byte[] b = new byte[i];
fis.read(b,0,i);
fis.close();
fis = null;
f = null;
System.gc() ;
return b;
}
}
//=========================================================================
如果上面程序存为: Class1.jws 文件,直接复制到:
D:\dotNet.J2EE\resin-2.1.16\webapps\axis\Class1.jws
或
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\webapps\axis\Class1.jws
即可通过 IE 直接访问如下 URL 地址:
http://localhost:1080/axis/Class1.jws
这就如同 .Net Web Service .asmx 的前代码方式
接下来着重介绍一下定制部署 Java Web Service
将上面程序存为: Class1.java 文件,并用如下命令行编译:
javac.exe Class1.java
生成的 Class1.class 文件复制到:
D:\dotNet.J2EE\resin-2.1.16\webapps\axis\WEB-INF\classes\Class1.class
或
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\webapps\axis\WEB-INF\classes\Class1.class
编写部署文件
deploy.wsdd (undeploy.wsdd): 在通过如下命令行部署该 Web Service ,此时 Java Web Application Server 应已经运行:
java -cp %axis_lib%;%classpath%;. org.apache.axis.client.AdminClient deploy.wsdd -p1080
如果部署成功屏幕将会输出:
<service name="Class1Service" provider="java:RPC">
<parameter name="className" value="Class1"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
<Admin>Done processing</Admin>
如果在 Resin 上部署失败,可在:
D:\Resin\resin-2.1.16\conf\resin.conf
中:
后添加如下配置:
<system-property javax.xml.transform.TransformerFactory = "org.apache.xalan.processor.TransformerFactoryImpl" />
<system-property javax.xml.parsers.DocumentBuilderFactory = "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl" />
<system-property javax.xml.parsers.SAXParserFactory = "org.apache.xerces.jaxp.SAXParserFactoryImpl" />
<system-property org.xml.sax.driver = "org.apache.xerces.parsers.SAXParser" />
Tomcat 上暂没发现上面部署失败问题!
部署成功后,在下面文件:
D:\dotNet.J2EE\resin-2.1.16\webapps\axis\WEB-INF\server-config.wsdd
或
D:\dotNet.J2EE\jakarta-tomcat-5.5.9\webapps\axis\WEB-INF\server-config.wsdd
中,已经被自动添加如下配置:
<parameter name="allowedMethods" value="*"/>
<parameter name="className" value="Class1"/>
</service>
当然也可以手工直接填写配置 server-config.wsdd 而不使用命令行工具!
至此如不出意外 java Web Service 已经部署完毕!
Client Side:
http://localhost:1080/axis/services/Class1Service?wsdl
已经可以正常访问!
java Client:
还可以用解压 axis-bin-1_2_1.zip 后:
D:\dotNet.J2EE\axis-bin-1_2_1\axis-1_2_1\samples\client\DynamicInvoker.java
用如下命令行编译后:
javac -cp %classpath%;%axis_lib%;. -Xlint:unchecked DynamicInvoker.java
生成 DynamicInvoker.class,
再如下命令行运行:
java -cp %axis_lib%;%classpath%;. DynamicInvoker http://localhost:1080/axis/services/Class1Service?wsdl SayHelloTo 小平
其中 %axis_lib% 为环境变量! 其中 %classpath% 为环境变量!
测试如果正常则输出:
Preparing Axis dynamic invocation
Executing operation SayHelloTo with parameters:
in0=小平
SayHelloToReturn=你好: 小平
Done!
.Net Client:
先用如下命令生成代理类的程序 C# 代码:
% Visual Studio .Net 2003 安装目录下的 %\SDK\v1.1\Bin\wsdl.exe
具体命令行如下:
wsdl.exe /l:CS /out:Class1ServiceProxy.cs http://localhost:1080/axis/services/Class1Service?wsdl
然后用如下命令编译生成程序集: Class1ServiceProxy.dll
csc /t:library Class1ServiceProxy.cs 然后用如下命令编译生成程序集: Class1ServiceClient.exe
csc Class1ServiceClient.cs /r:Class1ServiceService.dll
using System.IO;
public class Class1
{
static void Main(string[] args)
{
//Download(ServerSidepath, ClientSidePath)
//Download(@"客户端本地路径", @"服务器端路径");
Download(@"d:\server\download\editplus.rar", @"e:\download_editplus.rar");
System.Console.WriteLine("down End");
System.Console.WriteLine("同步 up file exec ");
UploadFile(@"d:\Northwind.mdb");
System.Console.WriteLine("同步 up file End\n");
System.Console.WriteLine("异步 up chunks exec ");
UploadFileChunks(@"d:\editplus.rar", 64);
System.Console.ReadLine();
}
public static void UploadFile(string LocalFileName)
{
Class1Service xx = new Class1Service();
FileStream fs = new FileStream(LocalFileName, FileMode.Open); //Client Side Path
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
//调用 "同步执行" 的本地 Web Sevices 代理类的 方法,相当于同步调用了 Web Method !
xx.UploadFileBytes(buffer, System.IO.Path.GetFileName(LocalFileName));
}
//指定要上传的本地文件的路径,及每次上传文件块的大小
public static void UploadFileChunks(string LocalFileName,int ChunkSize)
{
Class1Service xx = new Class1Service();
string filename = System.IO.Path.GetFileName(LocalFileName);
FileStream fs = new FileStream(LocalFileName, FileMode.Open); //Client Side Path
//fs = File.OpenRead(LocalFileName);
int r = (int) fs.Length; //用于记录剩余还未上传的字节数,初值是文件的大小
//调用 "同步执行" 的本地 Web Sevices 代理类的 方法,相当于同步调用了 Web Method !
//预定服务器端空间
xx.CreateBlankFile(filename,r);
int size = ChunkSize * 1024;
int k = 0; //用于记录已经上传的字节数
i++; //用于记录上传的文件块数
while (r >= size)
{
byte[] buffer = new byte[size];
fs.Read(buffer,0,buffer.Length);
//调用 "异步执行" 的本地 Web Sevices 代理类的 方法,相当于异步调用了 Web Method !
//该 buffer 的字节要写到 服务器端 相应文件的从 Position = k 开始的字节
xx.BeginUploadFileChunkBytes(buffer,k,filename,new AsyncCallback(UploadFileChunkCallback),xx);
k += size;
r -= size;
i++;
}
if (r > 0) //剩余的零头
{
byte[] buffer = new byte[r];
fs.Read(buffer,0,buffer.Length);
//调用 "异步执行" 的本地 Web Sevices 代理类的 方法,相当于异步调用了 Web Method !
//该 buffer 的字节要写到 服务器端 相应文件的从 Position = k 开始的字节
xx.BeginUploadFileChunkBytes(buffer,k,filename,new AsyncCallback(UploadFileChunkCallback),xx);
i++;
}
fs.Close();
}
private static int i = -1; //用于记录上传的文件块数
private static void UploadFileChunkCallback(IAsyncResult ar)
{
Class1Service x = (Class1Service) ar.AsyncState;
Console.WriteLine(x.EndUploadFileChunkBytes(ar));
if ( --i == 0)
{
Console.WriteLine("异步 up all chunks end");
}
}
public static void Download(string ServerSideFileName,string LocalFileName)
{
Class1Service xx = new Class1Service();
byte[] b = xx.DownloadFileBytes(ServerSideFileName); //Server Side Path
FileStream fs = new FileStream(LocalFileName, FileMode.Create); //Client Side Path
fs.Write(b,0,b.Length);
fs.Close();
}
}
运行 Class1ServiceClient.exe 即可!
运行结束后请检查 D:\Server\Upload\ 目录中是否已有文件上传!