现在的位置: 首页 > 综合 > 正文

VB.NET客户端调用Axis的Java Web Service实现文件上传

2012年09月17日 ⁄ 综合 ⁄ 共 3959字 ⁄ 字号 评论关闭

1. Java Web Service类DocumentFileManagement:

import java.io.*;
/**
 * @author tyrone
 *
 */

public class DocumentFileManagement {

//文件名, 文件内容,保存到d盘根目录
 public String saveFile(String filename,byte[] contents){
  if (filename==null || contents==null)
   return "null";
  String filepath="d://";
  File fp=new File(filepath+filename);
  try{
   BufferedOutputStream op=new BufferedOutputStream(new FileOutputStream(fp));
   op.write(contents,0,contents.length);
   op.flush();
   op.close();
   if(fp.exists())
    return fp.getAbsolutePath();
   else
    return "failure to create";
  }catch(java.io.IOException e){
   return e.getMessage();
  }
 }
}

2 部署到Axis服务器:

生成depoy.wsdd

<deployment xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
 <service name="DocumentFileManagement" provider="java:RPC">
  <parameter name="className" value="DocumentFileManagement"/>
  <parameter name="allowedMethods" value="saveFile"/>
 </service>
</deployment>

命令行:/>AdminClient  depoy.wsdd

3. copy DocumentFileManagement .class 到 axis/WEB-INF/classes目录

4. 用.NET的wsdl.exe生成VB.NET客户端类

命令行:/>wsdl /language:vb http://localhost:8080/axis/services/DocumentFileManagement?wsdl

生成DocumentFileManagementService.vb

'------------------------------------------------------------------------------
' <autogenerated>
'     This code was generated by a tool.
'     Runtime Version: 1.1.4322.573
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </autogenerated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On

Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization

'
'This source code was auto-generated by wsdl, Version=1.1.4322.573.
'

'<remarks/>
<System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Web.Services.WebServiceBindingAttribute(Name:="DocumentFileManagementSoapBinding", [Namespace]:="http://localhost:8080/axis/services/DocumentFileManagement")>  _
Public Class DocumentFileManagementService
    Inherits System.Web.Services.Protocols.SoapHttpClientProtocol
   
    '<remarks/>
    Public Sub New()
        MyBase.New
        Me.Url = "http://localhost:8080/axis/services/DocumentFileManagement"
    End Sub
   
    '<remarks/>
    <System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace:="http://DefaultNamespace", ResponseNamespace:="http://localhost:8080/axis/services/DocumentFileManagement")>  _
    Public Function saveFile(ByVal in0 As String, <System.Xml.Serialization.SoapElementAttribute(DataType:="base64Binary")> ByVal in1() As Byte) As <System.Xml.Serialization.SoapElementAttribute("saveFileReturn")> String
        Dim results() As Object = Me.Invoke("saveFile", New Object() {in0, in1})
        Return CType(results(0),String)
    End Function
   
    '<remarks/>
    Public Function BeginsaveFile(ByVal in0 As String, ByVal in1() As Byte, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
        Return Me.BeginInvoke("saveFile", New Object() {in0, in1}, callback, asyncState)
    End Function
   
    '<remarks/>
    Public Function EndsaveFile(ByVal asyncResult As System.IAsyncResult) As String
        Dim results() As Object = Me.EndInvoke(asyncResult)
        Return CType(results(0),String)
    End Function
End Class

5. 调用DocumentFileManagementService.vb
Module Module1

    Sub Main()
        Dim ws As New DocumentFileManagementService
        Dim result As String

        'connect colimas web service
        Dim contents As Byte
        Dim fp As System.IO.File
        Dim str As System.IO.FileStream
        Dim fpname As String

        Dim i As Integer

        '上传的文件名
        fpname = "StevenMills.doc"
        str = fp.Open(fpname, 3)
        Dim content(str.Length) As Byte
        For i = 0 To str.Length - 1
            content(i) = str.ReadByte()
        Next
        Try

           '调用WebService的方法
            result = ws.saveFile(fpname, content)
            Console.Write(result)
        Catch ex As Exception
            Console.Write(ex.Message())
        End Try

    End Sub

End Module

抱歉!评论已关闭.