文件上传下载webservice

原创 2004年08月18日 09:53:00

Imports System.Web.Services
Imports System.Configuration
Imports System.IO


<WebService(Namespace:="http://www.dynamics-apps.com")> _
Public Class FileServer
    Inherits System.Web.Services.WebService

#Region " Web Services Designer Generated Code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Web Services Designer.
        InitializeComponent()

        'Add your own initialization code after the InitializeComponent() call
    End Sub

    'Required by the Web Services Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Web Services Designer
    'It can be modified using the Web Services Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: This procedure is required by the Web Services Designer
        'Do not modify it using the code editor.
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region

    Private Shared WebServerPath As String = ConfigurationSettings.AppSettings("WebServerRootPath")

    Public Enum FileExtensions
        htm
        html
        asp
        aspx
        jpg
        gif
        dll
        exe
        all
    End Enum

    Public Class FileInformation
        Public Name As String
        Public Size As Long
        Public CreadedDate As DateTime
        Public LastModified As DateTime
        Public LastAccess As DateTime
        Public FileType As String
        Public FileLocation As String
        Public FileContent As Byte()
    End Class

    <WebMethod(Description:="Retrieve a boolean indicating if Path exists or not in WebServer")> _
    Public Function DirectoryExists(ByVal Path As String) As Boolean
        Dim di As New DirectoryInfo(WebServerPath & Path)
        Return di.Exists
    End Function

    <WebMethod(Description:="Retrieve a boolean indicating if File exists or not in WebServer")> _
    Public Function FileExists(ByVal Path As String, ByVal FileName As String) As Boolean
        Dim di As DirectoryInfo
        Dim fi As FileInfo
        Try
            di = New DirectoryInfo(WebServerPath & Path)
            If Not di.Exists Then
                Return False
                Exit Function
            End If
            Dim afi As FileInfo() = di.GetFiles()
            For Each fi In afi
                If fi.Name = FileName Then
                    Return True
                    Exit For
                End If
            Next
        Catch
            Return False
        End Try

    End Function

    <WebMethod(Description:="Retrieve an array of files with name, attributes and content.")> _
    Public Function Browse(ByVal VirtualPath As String, ByVal FileExtension As FileExtensions) As FileInformation()
        Dim i As Integer
        Dim fi As FileInfo
        Dim aFiles As FileInformation()
        Dim mExtension As String
        Select Case FileExtension
            Case FileExtensions.asp
                mExtension = "asp"
            Case FileExtensions.aspx
                mExtension = "aspx"
            Case FileExtensions.gif
                mExtension = "gif"
            Case FileExtensions.htm
                mExtension = "htm"
            Case FileExtensions.html
                mExtension = "html"
            Case FileExtensions.jpg
                mExtension = "jpg"
            Case FileExtensions.dll
                mExtension = "dll"
            Case FileExtensions.exe
                mExtension = "exe"
            Case FileExtensions.all
                mExtension = "*"
        End Select
        Dim di As New DirectoryInfo(WebServerPath & VirtualPath)
        Dim afi As FileInfo() = _
         di.GetFiles("*." & mExtension)
        ReDim Preserve aFiles(afi.Length - 1)
        For Each fi In afi
            aFiles(i) = New FileInformation()
            aFiles(i).Name = fi.Name
            aFiles(i).Size = fi.Length
            aFiles(i).LastAccess = fi.LastAccessTime
            aFiles(i).CreadedDate = fi.CreationTime
            aFiles(i).LastModified = fi.LastWriteTime
            aFiles(i).FileType = fi.Extension
            aFiles(i).FileLocation = fi.DirectoryName
            aFiles(i).FileContent = ReadFile(WebServerPath & VirtualPath & "/" & fi.Name)
            i += 1
        Next
        Return aFiles
    End Function

    <WebMethod(Description:="Retrieve a single file.")> _
    Public Function GetFile(ByVal VirtualPath As String, ByVal FileName As String) As Byte()
        Return ReadFile(WebServerPath & VirtualPath & "/" & FileName)
    End Function

    Private Shared Function ReadFile(ByVal FilePath As String) As Byte()
        Dim fs As FileStream
        Try
            ' Read file and return contents
            fs = File.Open(FilePath, FileMode.Open, FileAccess.Read)
            Dim lngLen As Long = fs.Length
            Dim abytBuffer(CInt(lngLen - 1)) As Byte
            fs.Read(abytBuffer, 0, CInt(lngLen))
            Return abytBuffer
        Catch exp As Exception
            Return Nothing
        Finally
            If Not fs Is Nothing Then
                fs.Close()
            End If
        End Try
    End Function

    <WebMethod(Description:="Upload a single file to web server.")> _
    Public Function UploadFile(ByVal VirtualPath As String, ByVal Name As String, ByVal Content As Byte()) As Boolean
        Dim objFile As File, objStream As StreamWriter, objFstream As FileStream
        Try
            objFstream = File.Open(WebServerPath & VirtualPath & "/" & Name, FileMode.Create, FileAccess.Write)
            Dim lngLen As Long = Content.Length
            objFstream.Write(Content, 0, CInt(lngLen))
            objFstream.Flush()
            objFstream.Close()
            Return True
        Catch exc As System.UnauthorizedAccessException
            Return False
        Catch exc As Exception
            Return False
        Finally
            If Not objFstream Is Nothing Then
                objFstream.Close()
            End If
        End Try
    End Function


#Region " Helper Function "
    <WebMethod(Description:="Retrieves version & copyright information about this web service.")> Public Function About() As String
        ' Uses the StringWriter to build a string with carriage returns & line feeds to
        ' return back to a calling client the Title, Version, and Description by
        ' reading Assembly meta-data originally entered in the AssemblyInfo.vb file
        ' using the AssemblyInfo class defined in the same file.

        Try
            Dim sw As New System.IO.StringWriter()
            Dim ainfo As New AssemblyInfo()

            With sw
                .WriteLine(ainfo.Title)
                .WriteLine(String.Format("Version {0}", ainfo.Version))
                .WriteLine(ainfo.Copyright)
                .WriteLine("")
                .WriteLine(ainfo.Description)
                .WriteLine("")
                .WriteLine(ainfo.CodeBase)
            End With

            Dim strRetVal As String = sw.ToString
            sw.Close()

            Return strRetVal
        Catch exp As Exception
            Return exp.Message
        End Try
    End Function
#End Region

End Class

webservice jersey 文件上传

webservice上传 在项目中整合spring可以使用 @POST @Consumes(MediaType.MULTIPART_FORM_DATA) // 消费注解必须是这个类型 ...
  • u013614451
  • u013614451
  • 2014年11月06日 22:28
  • 1044

WebService 实现文件的上传下载(非自动生成)

初入webservice的时候第一个接触的工具叫axis2,网上有着大量的简单案例。功能很强大,代码自动生成,能传递各种类型的数据。但是考虑到整合入公司项目的问题,决定去找一个不是自动生成手写的web...
  • kokoyuo
  • kokoyuo
  • 2016年07月27日 14:57
  • 2136

.net 写文件上传下载webservice

using System; using System.Collections; using System.ComponentModel; using System.Data; using Sy...
  • cw370008359
  • cw370008359
  • 2013年04月11日 10:32
  • 635

webservice实现文件上传下载(小文件)

测试环境:axis2-1.6.1、6.0.20、jdk1.5   说明:本方式仅适用于文件小于10M的场景(否则会出现内存溢出),大文件的上传下载应另选其他方式。   1、创建要发布成webs...
  • qq_29663071
  • qq_29663071
  • 2016年07月05日 09:20
  • 154

[JS]实现文件上传(二)

1.CSS & JS       .file{    position: relative;    background-color: #b32b1b;    border: 1px solid...
  • wobaiwodedukuku
  • wobaiwodedukuku
  • 2016年11月23日 15:31
  • 833

jersey rest webservice实现上传下载

package com.teamhow.daoimpl; import java.io.BufferedInputStream; import java.io.File; import ja...
  • xiaoseqingchun
  • xiaoseqingchun
  • 2014年08月16日 21:42
  • 2661

webservice文件上传下载(byte[] 实现方式)

测试环境:axis2-1.6.1、6.0.20、jdk1.5
  • u010395804
  • u010395804
  • 2014年06月17日 16:08
  • 764

java—(6)OpenAdaptor:基于webservice传输大文件

OpenAdaptor支持XFire和CXF开发的webservice,所以开发CXF webservice,基于myeclipse开发比较容易。参照网上webrvice传输大文件示例修改 http:...
  • szh1124
  • szh1124
  • 2015年05月22日 11:42
  • 1574

webservices系列(二)——JAX-WS文件上传下载

新建ImgData类,存放文件javabean DataHandler:使用这个类型存放文件 @XmlRootElement(name="ImaData") @XmlAccessorType(XmlA...
  • luo4105
  • luo4105
  • 2017年04月09日 22:41
  • 977

Java实现文件上传下载功能

一、原理介绍 1.文件上传原理 在TCP/IP中,最早出现的文件上传机制是FTP。它是将文件由客户端发送到服务器的标准机制。但是在JSP编程过程中不能使用FTP方法来上传文件,这是由JSP的运行...
  • Kiss_The_sky
  • Kiss_The_sky
  • 2017年03月12日 22:14
  • 599
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:文件上传下载webservice
举报原因:
原因补充:

(最多只允许输入30个字)