zoukankan      html  css  js  c++  java
  • [1.0]用WebService构建简单的2进制流Mp3下载服务.

    整理了一下下,代码很简单,只是涉及了简单的文件流转化

    至于文件路径请记得修改.在.Net 2005 Express Beta下调试通过.

    <%@ WebService Language="C#" Class="GetBinaryFile" %>

    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Web.UI;
    using System.IO;



    [WebService(Namespace 
    = "http://localhost/WebMp3Downloads/", Description = "在WebServices里利用Dot.Net框架进行文件传输")]
    public class GetBinaryFile  : System.Web.Services.WebService {

        [WebMethod]
        
    public string HelloWorld() {
            
    return "Hello World";
        }

        [WebMethod(Description 
    ="提供MP3的2进制流下载服务")]
        
    public byte[] GetMp3File(string requestFileName)
        
    {
            
    //得到服务器端的一个文件
            if (requestFileName == null||requestFileName==string.Empty)
            
    {
                
    return getBinaryFile("D:\\1.mp3");
            }

            
    else
            
    {
                
    return getBinaryFile("D:\\" + requestFileName);
            }

        }


        
    /// <summary>
        
    /// 将文件转化成2进制流 ConvertFileStreamToByteBuffer
        
    /// </summary>
        
    /// <param name="filename"></param>
        
    /// <returns></returns>

        public byte[] getBinaryFile(string filename)
        
    {
            
    if (File.Exists(filename))
            
    {
                
    try
                
    {
                    
    //打开现有文件以进行读取。
                    FileStream s = File.OpenRead(filename);
                    
    return this.ConvertStreamToByteBuffer(s);
                }

                
    catch
                
    {
                    
    return new byte[0];
                }

            }

            
    else
            
    {
                
    return new byte[0];
            }

        }


        
    /// <summary>
        
    /// 将流文件转化成2进制字节数组Convert FileStream Into Byte
        
    /// </summary>
        
    /// <param name="theStream"></param>
        
    /// <returns></returns>

        public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
        
    {
            
    int b1;
            System.IO.MemoryStream tempStream 
    = new System.IO.MemoryStream();

            
    while ((b1 = theStream.ReadByte()) != -1)
            
    {
                tempStream.WriteByte(((
    byte)b1));
            }

            
    return tempStream.ToArray();
        }


        
    /// <summary>
        
    /// 返回传送文件的文件类型 Get File Type of the Specific File
        
    /// </summary>
        
    /// <returns></returns>

        [WebMethod(Description = "返回传送文件的文件类型")]
        
    public string GetFileType(string fileName)
        
    {
            
    return "Text/txt";
        }

        

    }


    本文来自博客园,作者:Slashout,转载请注明原文链接:https://www.cnblogs.com/SlashOut/archive/2005/03/31/129237.html 关注公众号:数字化转型

  • 相关阅读:
    sql两个字段相加减,第三个字段没有值的原因.
    CF 447B(DZY Loves Strings-贪心)
    Appium AndroidKeyCode
    初探Java多线程
    模拟实现Spring IoC功能
    Cordova 5 架构学习 Weinre远程调试技术
    快学Scala习题解答—第三章 数组相关操作
    org.hibernate.AssertionFailure: null id in com.you.model.User entry (don&#39;t flush the Session after a
    Swift3.0语言教程替换子字符串
    DHCP欺骗(DHCP Sproofing)
  • 原文地址:https://www.cnblogs.com/SlashOut/p/129237.html
Copyright © 2011-2022 走看看