`
wlh269
  • 浏览: 447191 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

axis2和JDK1.5开发(文件传输服务)详解二(图解)

    博客分类:
  • J2SE
阅读更多
5.生成webservice服务端代码
  在package explorer窗口中选择右键new--others--Axis2 Service Archiver
 

下一步,指定class文件的位置:


下一步,选择SKIP WSDL
http://dl.iteye.com/upload/attachment/270302/78b8d660-c5ee-388e-bdba-7748d7ea199f.jpg[/img]

下一步,默认跳过


指定services.xml文件路径:


指定输出aar包的位置:


如果弹出成功界面那么生成成功!
[img]http://dl.iteye.com/upload/attachment/270302/78b8d660-c5ee-388e-bdba-7748d7ea199f.jpg [/img]



6.发布webservice服务
    将生成的 *.aar包拷贝到,%TOMCAT_HOME%\webapps\axis2\WEB-INF\services\下,启动tomcat即可
7.测试webservice服务
    --------上传文件代码段------

         //调用WebService服务上传待发信息的文件 
        if(SystemParameter.IsSendSMS){ 
        IOUtil.appendStr2File(SystemParameter.smsSendDir +File.separator+ "sms_send_list_" + sendtime+ ".txt","EOF", true, IOUtils.LINE_SEPARATOR_WINDOWS); 
        File uploadFile=new File(SystemParameter.smsSendDir +File.separator+ "sms_send_list_" + sendtime+ ".txt"); 
        if(uploadFile.exists()){ 
        boolean hasUpload= fileClient.upload("sms_send_list_" + sendtime,uploadFile, "txt"); 
        if(hasUpload){ 
        LOG.info("上传待发催报短信息文件:"+uploadFile.getName()+"成功!"); 
        try { 
Thread.sleep(10000); 
} catch (InterruptedException e) { 
e.printStackTrace(); 
} 
//删除已经上传的信息文件 
           boolean flag =delFile(uploadFile); 
           if(flag){ 
           LOG.info("删除待发催报短信息文件:"+uploadFile.getName()+"成功!");   
           } 
           
        }else{ 
        LOG.info("上传待发催报短信息文件:"+uploadFile.getName()+"失败!"); 
        } 
        } 
         } 





------下载文件代码段-------

  package com.hollyinfo.invest.biz.message; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.RandomAccessFile; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.TimerTask; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import com.hollyinfo.SystemParameter; 
import com.hollyinfo.hiiap.common.logger.HollyinfoLog; 
import com.hollyinfo.hiiap.common.logger.HollyinfoLogger; 
import com.hollyinfo.invest.dao.message.MessageDao; 
import com.hollyinfo.invest.model.message.MessageReceiver; 
import com.hollyinfo.webservice.SMSFileTransClient; 

/** 
* 短信发送状态定时查询并更新该状态到系统 
* @author wulihai 
* @date 2010-06-09 
*/ 
public class SMSQueryBizImpl extends TimerTask implements SMSQueryBiz  { 
/** 日志初始化 */ 
private static HollyinfoLog LOG = HollyinfoLogger.getLog(MessageBizImpl.class); 
private SMSFileTransClient fileClient; 
public void setFileClient(SMSFileTransClient fileClient) { 
this.fileClient = fileClient; 
} 
/** 消息数据通道接口 */ 
private MessageDao messageDao; 

public void setMessageDao(MessageDao messageDao) { 
this.messageDao = messageDao; 
} 
public SMSQueryBizImpl() { 

} 
public void run() { 
try{ 
//查询待接收结果文件 
    List fileNames=fileClient.queryFileList(); 
    for (Iterator iterator = fileNames.iterator(); iterator.hasNext();) { 
String fileName = (String) iterator.next(); 
int idx=fileName.indexOf("."); 
String preName=fileName.substring(0, idx); 
//下载结果文件 
boolean flag=fileClient.download(preName, "txt"); 
if(flag){ 
LOG.info("下载结果文件"+fileName+","+flag); 
Thread.sleep(10000); 
boolean delflag=false; 
if(readLastLine(new File(SystemParameter.smsResultDir+File.separator+fileName),"utf-8").indexOf("EOF")!=-1){ 
//删除服务端已下载文件 
delflag=fileClient.deleteFile(preName, "txt"); 
//更新发送结果到核心系统 
updateSendResults(fileName); 
  } 
//删除客户端已下载文件 
delFile(new File(SystemParameter.smsResultDir+File.separator+fileName)); 
  
    LOG.info("删除中间服务结果文件"+fileName+","+delflag); 
    
    }else{ 
LOG.info("下载结果文件"+fileName+","+flag); 
} 
} 
    
    //======检查是否还有没上传成功的文件,做重新上传操作=====// 
    String [] filenames=new File(SystemParameter.smsSendDir).list(); 
    for (int i = 0; i  0) {   
        pos--;   
        raf.seek(pos);   
        if (raf.readByte() == '\n') {   
          break;   
        }   
      }   
      if (pos == 0) {   
        raf.seek(0);   
      }   
      byte[] bytes = new byte[(int) (len - pos)];   
      raf.read(bytes);   
      if (charset == null) {   
        return new String(bytes);   
      } else {   
        return new String(bytes, charset);   
      }   
    }   
  } catch (FileNotFoundException e) {   
  } catch (IOException e) { 
e.printStackTrace(); 
} finally {   
    if (raf != null) {   
      try {   
        raf.close();   
      } catch (Exception e2) {   
      }   
    }   
  }   
  return null;   
} 
public int[] updateSendResults(String fileName) { 
BufferedReader br=null; 
InputStreamReader isr=null; 
MessageReceiver ms=null; 
List receiverList =new ArrayList(); 
int flag[] = null; 
String tmpstr=""; 
try { 
File temp=new File(SystemParameter.smsResultDir); 
if(!temp.exists()){ 
temp.mkdirs(); 
} 
File temp2=new File(SystemParameter.smsResultDir+File.separator+fileName); 
if(!temp2.exists()){ 
temp2.createNewFile(); 
} 
isr = new InputStreamReader(new FileInputStream(temp2), "utf-8"); 
br=new BufferedReader(isr); 
while((tmpstr=br.readLine())!=null){ 
String [] str=tmpstr.split("\t"); 
if(str.length>=7){ 
ms=new MessageReceiver(); 
ms.setMsg_id(new Long(str[1])); 
ms.setReceive_time_sms(new Long(str[4]));//时间 
ms.setReceive_state_sms(new Integer(str[5]));//状态 
receiverList.add(ms); 
} 
} 
messageDao.updateSendResults(receiverList); 
} catch (UnsupportedEncodingException e) { 
e.printStackTrace(); 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
}finally{ 
try { 
if(br!=null){ 
br.close(); 
} 
if(isr!=null){ 
isr.close(); 
   } 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
return flag; 
} 
} 




---------WS客户端核心代码封装----------------

 package com.hollyinfo.webservice; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Vector; 

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

import org.apache.axiom.attachments.utils.IOUtils; 
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.AxisFault; 
import org.apache.axis2.Constants; 
import org.apache.axis2.addressing.EndpointReference; 
import org.apache.axis2.client.Options; 
import org.apache.axis2.client.ServiceClient; 

import com.hollyinfo.SystemParameter; 

/** 
* 文件传输客户端 
* @author wulihai 
* @Date 2010-06-08 
* 
*/ 
public class SMSFileTransClient { 
   private static EndpointReference targetEPR =new EndpointReference(SystemParameter.targetEPR); 
   
   public  boolean upload(String fileName, File file, String fileType) { 
     try { 
      OMElement data = buildUploadEnvelope(fileName, file, fileType); 
      Options options = buildOptions(); 
      ServiceClient sender = new ServiceClient(); 
      sender.setOptions(options); 
      OMElement ome = sender.sendReceive(data); 
      String b = ome.getText(); 
      return Boolean.valueOf(b).booleanValue(); 
     } 
     catch(Exception e) { 
       e.printStackTrace(); 
     } 
     return false; 
   } 
   
   public  boolean download( String fileName, String fileType) { 
   FileOutputStream ops=null; 
   InputStream ips=null; 
     try { 
       OMElement data = buildDownloadEnvelope( fileName, fileType); 
       Options options = buildOptions(); 
       ServiceClient sender = new ServiceClient(); 
       sender.setOptions(options); 
       OMElement ome = sender.sendReceive(data); 
       OMText binaryNode = (OMText) ome.getFirstOMChild(); 
       binaryNode.setOptimize(true); 
       DataHandler actualDH = (DataHandler) binaryNode.getDataHandler(); 
       File tmpDir=new File(SystemParameter.smsResultDir); 
       if(!tmpDir.exists()){ 
       tmpDir.mkdirs(); 
       } 
       //下载保存文件 
       File tmpfile=new File(SystemParameter.smsResultDir+File.separator+fileName+"."+fileType); 
       if(!tmpfile.exists()){ 
       tmpfile.createNewFile(); 
       } 
        ops = new FileOutputStream(tmpfile); 
        ips = actualDH.getInputStream(); 
       //每次读取10KB,写到输出流 
       int bytesRead=0; 
       byte[] buf = new byte[10*1024]; //10KB 
       while((bytesRead = ips.read(buf)) != -1){ 
       ops.write(buf, 0, bytesRead); 
        } 
       return true; 
      } 
      catch(Exception e) { 
        e.printStackTrace(); 
      }finally{ 
    try { 
      if(ips!=null){ 
ips.close(); 
    } 
      if(ops!=null){ 
      ops.close(); 
     } 
} catch (IOException e) { 
e.printStackTrace(); 
} 
      
      } 
     return false; 
   } 
   
   /** 
    * 删除指定文件 
    * @param fileName 
    * @param fileType 
    * @return 
    */ 
   public  boolean deleteFile( String fileName, String fileType) { 
   boolean flag=false; 
       try { 
         OMElement data = buildDeleteEnvelope(fileName, fileType); 
         Options options = buildOptions(); 
         ServiceClient sender = new ServiceClient(); 
         sender.setOptions(options); 
         OMElement ome = sender.sendReceive(data); 
         for (Iterator _iterator = ome.getChildElements(); _iterator.hasNext();) { 
             OMElement _ele = (OMElement) _iterator.next(); 
             if (_ele.getLocalName().equalsIgnoreCase("success")) { 
            flag=Boolean.valueOf(_ele.getText()).booleanValue() ; 
             } 
          } 
        } 
        catch(Exception e) { 
          e.printStackTrace(); 
        } 
       return flag; 
     } 
   
   private static OMElement buildDeleteEnvelope(String fileName, String fileType) { 
     OMFactory fac = OMAbstractFactory.getOMFactory(); 
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd"); 
     OMElement data = fac.createOMElement("deleteFile", omNs); 
     OMElement _fileName = fac.createOMElement("fileName", omNs); 
     _fileName.setText(fileName); 
     OMElement _fileType=fac.createOMElement("fileType", omNs); 
     _fileType.setText(fileType); 
     data.addChild(_fileName); 
     data.addChild(_fileType); 
     return data; 
   } 


   //得到当前的结果文件 
   public List  queryFileList(){ 
   Vector vec=new Vector(); 
   try { 
   OMElement data = buildQuerydEnvelope(); 
   Options options = buildOptions(); 
   ServiceClient sender = new ServiceClient(); 
       sender.setOptions(options); 
       OMElement ome = sender.sendReceive(data); 
      // OMElement firstElement=ome.getFirstElement();   
           for (Iterator _iterator = ome.getChildElements(); _iterator.hasNext();) { 
               OMElement _ele = (OMElement) _iterator.next(); 
               if (_ele.getLocalName().equalsIgnoreCase("file")) { 
               vec.add(_ele.getText()); 
               } 
            } 
   }catch(Exception e) { 
        e.printStackTrace(); 
   } 
return vec; 
   } 
   
   private static OMElement buildQuerydEnvelope() { 
   /************创建调用webservice接口所需的参数OMElement对象*************/  
       OMFactory fac=OMAbstractFactory.getOMFactory();   
       OMNamespace omNs=fac.createOMNamespace("http://example.org/filedata", "fd");   
      //data中包含了需要调用的web service的方法名称,其中queryFileList就是其中的一个方法   
       OMElement data=fac.createOMElement("queryFileList",omNs);   
       OMElement _flag = fac.createOMElement("flag", omNs); 
       _flag.setText("true"); 
       data.addChild(_flag); 
       return data;    
   } 
   
   private static OMElement buildUploadEnvelope(String fileName, File file, String fileType) { 
     DataHandler expectedDH; 
     OMFactory fac = OMAbstractFactory.getOMFactory(); 
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd"); 
     OMElement data = fac.createOMElement("upload", omNs); 
     OMElement fileContent = fac.createOMElement("fileContent", omNs); 
     FileDataSource dataSource = new FileDataSource(file); 
     expectedDH = new DataHandler(dataSource); 
     OMText textData = fac.createOMText(expectedDH, true); 
     fileContent.addChild(textData); 
     OMElement _fileName = fac.createOMElement("fileName", omNs); 
     _fileName.setText(fileName); 
     OMElement _fileType = fac.createOMElement("fileType", omNs); 
     _fileType.setText(fileType); 
     data.addChild(_fileName); 
     data.addChild(_fileType); 
     data.addChild(fileContent); 
     return data; 
   } 
   
   private static OMElement buildDownloadEnvelope( String fileName, String fileType) { 
     OMFactory fac = OMAbstractFactory.getOMFactory(); 
     OMNamespace omNs = fac.createOMNamespace("http://example.org/filedata", "fd"); 
     OMElement data = fac.createOMElement("download", omNs); 
     OMElement _fileName = fac.createOMElement("fileName", omNs); 
     _fileName.setText(fileName); 
     OMElement _fileType=fac.createOMElement("fileType", omNs); 
     _fileType.setText(fileType); 
     data.addChild(_fileName); 
     data.addChild(_fileType); 
     return data; 
   } 
   private static Options buildOptions() throws AxisFault { 
     Options options = new Options(); 
     options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); 
     options.setTo(targetEPR); 
     // enabling MTOM in the client side 
     options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE); 
     options.setTransportInProtocol(Constants.TRANSPORT_HTTP); 
     return options; 
   } 


   
} 

  • 大小: 49.5 KB
  • 大小: 35.8 KB
  • 大小: 27 KB
  • 大小: 37.8 KB
  • 大小: 33 KB
  • 大小: 41.7 KB
  • 大小: 48.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics