VB.NET用戶端調用Axis的Java Web Service實現檔案上傳

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.