首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

webService上传数据时服务器取到的数据不完整。解决思路

2013-03-26 
webService上传数据时服务器取到的数据不完整。客户端package cn.flyingsoft.oais.service.webService.clie

webService上传数据时服务器取到的数据不完整。
客户端

package cn.flyingsoft.oais.service.webService.client;
import java.io.File;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class LineFileInteropServiceStub1 {

 private static EndpointReference targetEPR =new EndpointReference("http://127.0.0.1:8080/esoaisapp/services/LineFileInteropService");   
  
 public static OMElement getMethod(DataHandler dh,String filePath,String fileName,String fileType) {   
        OMFactory fac = OMAbstractFactory.getOMFactory();   
        OMNamespace omNs = fac.createOMNamespace("http://webService.service.oais.flyingsoft.cn", "tns");   
  
        OMElement method = fac.createOMElement("lineFileUpload", omNs);
        
        OMElement value1 = fac.createOMElement("dh", omNs);
        OMElement value2= fac.createOMElement("fileContent", omNs);
        OMElement value3 = fac.createOMElement("fileName", omNs);
        OMElement value4 = fac.createOMElement("fileType", omNs);
        
        value1.addChild(fac.createOMText(dh, true));   
        value2.addChild(fac.createOMText(value2, filePath));   
        value3.addChild(fac.createOMText(value3, fileName));  
        value4.addChild(fac.createOMText(value4, fileType));  

        method.addChild(value2);   
        method.addChild(value3);  
        method.addChild(value4);
        method.addChild(value1); 
        
        return method;   
    }   
   
    public static void main(String[] args) {   
        try {   
        String filePath="d:/2012-09-24 13-23-05.zip";
        String fileName="2012-09-24 13-23-05";
        String fileType="zip";
        DataHandler dh = new DataHandler(new FileDataSource(filePath));       // ftpPath为 服务器名+虚拟路径


            OMElement authentication = getMethod(dh,filePath,fileName,fileType);   
            System.out.println(authentication);
            Options options = new Options();   
            options.setTo(targetEPR);   
            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);   
            org.apache.axis2.client.ServiceClient sender = new org.apache.axis2.client.ServiceClient();   
            sender.setOptions(options);   
            OMElement result = sender.sendReceive(authentication);   //调用服务  返回result
            OMElement returnEle = result.getFirstElement();
            System.out.println(returnEle.getText()) ;
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   


}
服务器
package cn.flyingsoft.oais.service.webService;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;


import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.ServiceLifeCycle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import cn.flyingsoft.oais.di.operate.IManager;
import cn.flyingsoft.oais.di.system.IUserManager;
import cn.flyingsoft.oais.di.system.entity.User;
import cn.flyingsoft.oais.ipe.PkgServer;
import cn.flyingsoft.oais.service.exchange.entity.impl.ZipUtil;
import cn.flyingsoft.oais.service.storage.StorageServer;
import cn.flyingsoft.oais.service.storage.manager.FtpClientManager;

/**
 * 离线文件上传.
 *  * 
 */
public class LineFileInteropService implements ServiceLifeCycle{

private static final long serialVersionUID = 1L;
private PkgServer pkgServer = null ;
private StorageServer storageServer = null ;
private User user = null ;
private ApplicationContext context = null ;
private FtpClientManager ftpClientManager = null ;
private IUserManager userManager = null ;


private IManager im = null ;
protected String[] files;
protected String xmlFilePath;
protected String zipFilePath;
protected String zipFullPath;

public LineFileInteropService(){
initServer();
}
public void initServer(){
//或许有更好的方法获取ApplicationContext 有空再研究-----记之
String classPath = LineFileInteropService.class.getProtectionDomain().getCodeSource().getLocation().getPath() ;
int pos = classPath.indexOf("WEB-INF") ;
String web_infPath = classPath.substring(1,pos+8) ; 
String serviceXml = web_infPath + "applicationContext-service.xml" ;
String resourcesXml = web_infPath + "applicationContext-resources.xml" ;
String[] locations = {resourcesXml, serviceXml};
context = new FileSystemXmlApplicationContext(locations);
pkgServer = (PkgServer) context.getBean("packageServer") ;
storageServer= (StorageServer)context.getBean("storageServer") ;
im = (IManager) context.getBean("manager");
}

/**
 * 文件上传.
 * @param element
 * @return
 */
public  OMElement lineFileUpload(OMElement element){
//存放.xml文件路径集合.
List<String> filePaths = new ArrayList<String>() ;
//文件内容.
OMElement _fileContent = null ;
//文件名.
OMElement _fileName = null ; 
//文件类型.
OMElement _fileType= null ;
//文件类型.
String type="";
try {

//获得目录名.
String storeDir = this.storageServer.getTempDataManager().getUserPath();
Iterator iterator = element.getChildElements();
while(iterator.hasNext()){

OMElement ele = (OMElement)iterator.next();
if(ele.getLocalName().equalsIgnoreCase("fileContent")){
_fileContent = ele;
}
if(ele.getLocalName().equalsIgnoreCase("fileName")){
_fileName = ele;
}
if(ele.getLocalName().equalsIgnoreCase("fileType")){
_fileType = ele;
}

}

if(null == _fileContent || null == _fileType){

throw new AxisFault("文件内容或文件名为空!");

}

OMText binaryNode = (OMText)_fileContent.getFirstOMChild();

String fileName = _fileName.getText();

String fileType = _fileType.getText();

File dir = new File(storeDir);

if(!dir.exists()){
dir.mkdirs();
}

String filePath = storeDir + "//" + fileName + "." + fileType;

File uploadFile = new File(filePath);
  
    if(uploadFile.exists()) {
 
    uploadFile.delete();
 
    uploadFile = new File(filePath);
 
    }
    
DataHandler actualDH = (DataHandler)binaryNode.getDataHandler();

InputStream input=actualDH.getInputStream();

FileOutputStream fileOutStream = new FileOutputStream(uploadFile);

byte[] buffer = new byte[1024 * 4];



int n=0;

while( (n=input.read(buffer)) !=-1 ){
fileOutStream.write(buffer, 0, n);
}
OMFactory fac = OMAbstractFactory.getOMFactory();   
    OMNamespace ns = fac.createOMNamespace("http://webService.service.oais.flyingsoft.cn","fd1");   
    OMElement ele = fac.createOMElement("response", ns);   
    ele.setText("true");   
    return ele;    

} catch (AxisFault e) {

e.printStackTrace();

}catch (Exception e){

e.printStackTrace();
}
return null;
}
public void shutDown(ConfigurationContext arg0, AxisService arg1) {
// TODO Auto-generated method stub
}
public void startUp(ConfigurationContext arg0, AxisService arg1) {
// TODO Auto-generated method stub
}
}
问题:
在客户段sender.sendReceive(authentication); authentication的数据是:
<tns:lineFileUpload xmlns:tns="http://webService.service.oais.flyingsoft.cn">
<tns:dh>UGAAAAAAIAAgBpAAAA1QUAAAAA</tns:dh>
<tns:fileContent>d:/2012-09-24 13-23-05.zip</tns:fileContent>
<tns:fileName>2012-09-24 13-23-05</tns:fileName>
<tns:fileType>zip</tns:fileType>
</tns:lineFileUpload>
但在服务器接收的参数值是:<tns:fileContent xmlns:tns="http://webService.service.oais.flyingsoft.cn">d:/2012-09-24 13-23-05.zip</tns:fileContent>也就是一个节点数据,不是完整的数据。
求高手指点。。。。。。。。。。。。。。。。。。。。。。。。
[解决办法]
OMElement 定义的结构不对了?只是看不太好分析。
最好让对方提供一个可以执行的java客户端,运行java客户端和你的C#客户端。用Fiddler2抓取Soap包,看看结构有什么不同。
[解决办法]
调用webservice,framework会帮你序列化,对方在反序列化,很可能这时候出的问题。因此要抓序列化后的数据进行比较。

热点排行