📄 client.txt
字号:
//说明:soap方式传输数据速度慢,本文利用自动压缩soap附件,再发送数据,进而提高soap传输速率!
//本例程用于客户端。
/**
* success! 用DataHandler实现soap的附件的自动压缩传输。
* DataHandler 是一种流数据。
*
* */
package axis.attachments.clientTest;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.util.ByteArrayDataSource;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory;
import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory;
import _MyUtil.Ln;
import _MyUtil.MyInputStream;
import _MyUtil.zip.GZIP;
import _MyUtil.zip.UnGZIP;
public class SentFile {
public static void main(String[] args) throws ServiceException, IOException {
// 接收soap附件
//sf.getFileFromServeTest(endpoint192_124); // test1 ()
//testGetFileFromServe();
// 发送、接收soap附件
testSendFileToServe(); // suceess:发送、不压缩
}
/**仅是处理收到的soap附件,附件未压缩。
* 处理:显示、保存到文件
* */
public void doRecv(DataHandler ret) throws IOException{
if(ret==null){
System.out.print("server return null!!");
}else{
try {
MyInputStream.saveStreamToFile(ret.getInputStream(),"c:\\","doRcvLk.txt"); // 保存到文件
//Ln.ln("DataSource Origin:\n"+MyInputStream.streamToString(ret.getInputStream())); // 显示
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**仅是处理收到的soap附件,附件被压缩。
* 处理:解压、并显示/保存到文件。(中文处理正确!!)
* */
public void doRecvAndDecompress(DataHandler ret){
UnGZIP ug= new UnGZIP();
if(ret==null){
System.out.print("server return null!!");
}else{
try {
//Ln.ln("decompress data :"+ug.deCompressStreamToString(ret.getInputStream())); // 显示解压后内容。
ug.deCompressStreamAndSaveToFile(ret.getInputStream(),"c:\\","doRecvDecompress.txt"); // 解压后内容保存到文件。
} catch (IOException e) {
e.printStackTrace();
}
}
}
/** 从服务器读取文件,要建立调用服务,以soap附件形式返回。
* */
public void getFileFromServeTest(String endpoint) throws ServiceException, IOException{
try {
MyCreateSoapDataHandler mySoap= new MyCreateSoapDataHandler();
mySoap.setParameter("filepath");
mySoap.setXmlType(XMLType.XSD_STRING);
mySoap.setParameterMode( ParameterMode.IN);
mySoap.createSoap(endpoint,"SendFileServer","sendFile"); // del lk
//mySoap.createSoap(endpoint,"SendFileServer","sendFileCompress"); // test jaxm servlet
//call.setReturnType(XMLType.SOAP_ARRAY); // 不能有该句,否则报错, add lk
//SentFile sf=new SentFile();
Service service=new Service();
Call call=(Call) service.createCall();
call.setReturnType(XMLType.SOAP_ARRAY);
long before ;
long after;
long between;
before = System.currentTimeMillis();
/**获取源文件为未压缩文件,未用解压缩处理的情况, success
* */
DataHandler ret = (DataHandler) mySoap.getCall().invoke(new Object[] {"c:\\lk.txt"});// success
this.doRecv(ret);
/**获取源文件为压缩文件,用解压缩处理的情况, success
* */
//DataHandler ret = (DataHandler) mySoap.getCall().invoke(new Object[] {"c:\\lk.zip"});
//this.doRecvDecompress(ret);
/**获取源文件为未压缩文件,用解压缩处理的情况,??
* */
//DataHandler ret = (DataHandler) mySoap.getCall().invoke(new Object[] {file});
//sf.doRecv(ret);
//sf.doRecvDecompress(ret);
after =System.currentTimeMillis();
between =after-before;
Ln.ln("Total time used :"+between);
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** 从服务器读取文件,以soap附件形式返回。?
* */
public DataHandler getFileFromServeEx(String file,String endpoint) throws ServiceException, IOException{
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName(endpoint, "getFile"));//指定方法的命名空间
QName qnameattachment=new QName("FileService","DataHandler");
call.registerTypeMapping(DataHandler.class,qnameattachment,JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class);
//call.addParameter("s1",qnameattachment,ParameterMode.IN);
call.addParameter("file",XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);//XMLType.XSD_STRING);//用Class.forName("java.lang.String")来获取java类型
DataHandler retdh=(DataHandler)call.invoke(new Object[] {file}); // 服务器上的文件名
//保存到本地
//MyInputStream.saveStreamToFile(retdh.getInputStream(),"c:\\","getFileFromServelk.txt");
return retdh;
// 显示内容
// Ln.ln("DataSource Origin:\n"+MyInputStream.streamToString(retdh.getInputStream())); // 显示
}
/** 从服务器读取文件
* */
public DataHandler getFileFromServe(String dstFile,String endpoint,String method) throws ServiceException, MalformedURLException, RemoteException{
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
//call.setOperationName(new QName("SendFileServer","sendFile")); //success,从服务器取非压缩文件
call.setOperationName(new QName("SendFileServer",method)); //从服务器取压缩文件
QName qnameAttachment = new QName("urn:SendFileServer",
"DataHandler");
call.registerTypeMapping(DataHandler.class, qnameAttachment,
JAFDataHandlerSerializerFactory.class,
JAFDataHandlerDeserializerFactory.class);
call.addParameter("filepath", XMLType.XSD_STRING, ParameterMode.IN);
// call.setReturnType(XMLType.SOAP_ARRAY);
DataHandler ret = (DataHandler) call
.invoke(new Object[] {dstFile});
return ret;
}
/** success,发送soap附件到服务器
* */
public void sendFileToServe(String fileClient,String fileServer,String endpoint) throws ServiceException, IOException{
Ln.ln("send fileClient:"+fileClient);
Ln.ln("send to fileServer:"+fileServer);
Ln.ln("endpoint:"+endpoint);
// 在客户端机器器上要上传的文件,该文件必须存在!!(可以为绝对、相对路径)
DataHandler dh=new DataHandler(new FileDataSource(fileClient)); //创建soap附件对象
Service service=new Service(); // 建立服务
Call call=(Call) service.createCall(); // 建立调用
call.setTargetEndpointAddress(new java.net.URL(endpoint)); // 设置服务URL
call.setOperationName(new QName(endpoint, "putFile")); //指定方法的命名空间
QName qnameattachment=new QName("FileService","DataHandler"); //制定URI和local part
call.registerTypeMapping(dh.getClass(),qnameattachment,JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class);//注册soap附件和命名URI,编码序列化
call.addParameter("s1",qnameattachment,ParameterMode.IN); // 设置输入参数
call.addParameter("s2",XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);// 设置返回数据类型 //XMLType.XSD_STRING);//用Class.forName("java.lang.String")来获取java类型
String ret=(String)call.invoke(new Object[] {dh,fileServer}); // 在服务器上要存储的文件名
System.out.println(ret);
}
/** sucess,压缩并发送soap附件到服务器,
* 压缩类型:文件
* */
public void sendAndCompressFileToServer(String srcFile,String endpoint) throws ServiceException, IOException{
Ln.ln("send file:"+srcFile);
Ln.ln("endpoint:"+endpoint);
String filename=srcFile; // 在客户端机器器上要上传的文件,该文件必须存在!!(可以为绝对、相对路径)
/** 构建DataHandler,有3种方法
* */
//方法1,sccess,用FileDataSource
// DataHandler dh=new DataHandler(new FileDataSource(filename));
//方法2,success,用ByteArrayDataSource,输入为byte[]、String、InputStream
byte[] bytes="abcd阿".getBytes();
GZIP gzip=new GZIP();
bytes=gzip.compressBytearrayToBytearray(bytes); // 压缩
DataHandler dh= new DataHandler(new ByteArrayDataSource(bytes,"application/octet-stream"));
//方法3,success,直接用DataHandler(0bject,String) ,可以构建String类型(但不能有汉字),但用byte[]构建不行
//DataHandler dh= new DataHandler("369我","text/plain");
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName(endpoint, "putFile"));//指定方法的命名空间
QName qnameattachment=new QName("FileService","DataHandler");
call.registerTypeMapping(dh.getClass(),qnameattachment,JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class);
call.addParameter("s1",qnameattachment,ParameterMode.IN);
call.addParameter("s2",XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);//XMLType.XSD_STRING);//用Class.forName("java.lang.String")来获取java类型
String ret=(String)call.invoke(new Object[] {dh,filename}); // 在服务器上要存储的文件名(不要带路径)
System.out.println(ret);
}
/** sucess,压缩并发送soap附件到服务器,
* 压缩类型:byte[],inputstream,string,(都用ByteArrayDataSource()构建DataHandler)然后传输。
* */
public void sendAndCompressObjectToServer(String destFile,String endpoint) throws ServiceException, IOException{
Ln.ln("endpoint:"+endpoint);
Ln.ln("\ndestFile:"+destFile);
//destFile; 在客户端机器器上要上传的文件,该文件必须存在!!(可以为绝对、相对路径)
/**构建DataHandler*/
//方法1,success,用ByteArrayDataSource,输入为byte[]、String、InputStream
byte[] bytes="abcd阿3333".getBytes();
GZIP gzip=new GZIP();
bytes=gzip.compressBytearrayToBytearray(bytes); // 压缩
DataHandler dh= new DataHandler(new ByteArrayDataSource(bytes,"application/octet-stream"));
//方法2,success,直接用DataHandler(0bject,String) ,可以构建String类型(但不能有汉字),但用byte[]构建不行
//DataHandler dh= new DataHandler("369我","text/plain");
Service service=new Service();
Call call=(Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName(new QName(endpoint, "doRecvDecompress"));//指定方法的命名空间
QName qnameattachment=new QName("FileService","DataHandler");
call.registerTypeMapping(dh.getClass(),qnameattachment,JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class);
call.addParameter("s1",qnameattachment,ParameterMode.IN);
//call.addParameter("s2",XMLType.XSD_STRING,ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);//XMLType.XSD_STRING);//用Class.forName("java.lang.String")来获取java类型
//String ret=(String)call.invoke(new Object[] {dh,destFile}); // 在服务器上要存储的文件名(不要带路径)
String ret=(String)call.invoke(new Object[] {dh}); // 在服务器上解压、显示
System.out.println(ret);
}
/** success. 获取,从服务器获取文件。
* @throws IOException
* @throws ServiceException
**/
public static void testGetFileFromServe() throws ServiceException, IOException{
SentFile sf= new SentFile();
String endpoint192_200 = "http://192.168.1.200:8080/axis/services/SendFileServer";
String dstFileOfServer="c:\\lk4.txt";
String serverMethodType="";
serverMethodType="sendFileAndCompress";
long timeBefor1=System.currentTimeMillis();
sf.doRecvAndDecompress(sf.getFileFromServe(dstFileOfServer,endpoint192_200,serverMethodType)); //success,解压服务器的压缩文件。
long timeAfter1=System.currentTimeMillis();
Ln.ln("server 传输方法:"+serverMethodType+",所用时间:"+(timeAfter1-timeBefor1)+" ms");
serverMethodType="sendFile";
long timeBefor=System.currentTimeMillis();
sf.doRecv(sf.getFileFromServe(dstFileOfServer,endpoint192_200,serverMethodType)); //success
long timeAfter=System.currentTimeMillis();
Ln.ln("server 传输方法:"+serverMethodType+",所用时间:"+(timeAfter-timeBefor)+" ms");
}
/** success ,发送文件到服务器
* @throws IOException
* @throws ServiceException
* */
public static void testSendFileToServe() throws ServiceException, IOException{
SentFile sf= new SentFile();
String endpoint192_200 = "http://192.168.1.200:8080/axis/services/SendFileServer";
sf.sendFileToServe("C:\\lk.zip","lkServ.zip",endpoint192_200); // success,发送文件
//sf.sendAndCompressObjectToServer("lk.txt",endpoint192_200); // success ,压缩、发送数据(可以为内存数据)到服务器
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -