[1.1]用WebService返回歌曲的曲目信息.借鉴[星集工作室 张麟 Dephi版]

Dephin源文请看 http://editblog.csdn.net/csdn/archive/2004/09/07/710.aspx 谢谢 张麟 同志的blog

.Net 版本 Web Service建立一个服务类代码
<%@ 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";
    }


    
    
/// <summary>
    
/// 返回传送文件的题头信息的类,以SOAP返回Tag类
    
/// </summary>
    
/// <returns></returns>

    [WebMethod(Description ="返回传送文件的题头信息的类,以SOAP返回类")]
    
public Mp3Tag GetMp3Taginfo(string fileName)
    
{
        
byte[] bMp3Tag = this.GetMp3File(fileName);       
         
        Mp3Tag mt
=new Mp3Tag(bMp3Tag);       

        
return mt;
    }

    
}




然后我们再添加一个存储了Mp3Tag题头的类 Mp3Tag.Class

using System;
using System.Text;

/// <summary>
/// Summary description for Mp3Tag
/// MP3的基本歌曲信息存在了MP3文件的最后128个字节里
/// 用于解读Mp3歌曲的题头信息
/// Sign Length
/// (bytes)  Position
///(bytes)  Description 
///A 3 (0-2) Tag identification. Must contain 'TAG' if tag exists and is correct. 
///B 30 (3-32) Title 
///C 30 (33-62) Artist 
///D 30 (63-92) Album 
///E 4 (93-96) Year 
///F 30 (97-126) Comment 
///G 1 (127) Genre 
/// </summary>

public class Mp3Tag
{
    
private byte[] TAGBody = new byte[128];
    
private byte[] sTag = new byte[3];
    
private byte[] sTitle = new byte[30];
    
private byte[] sArtist = new byte[30];
    
private byte[] sAlbum = new byte[30];
    
private byte[] sYear = new byte[4];
    
private byte[] sComment = new byte[30];
    
private byte[] sGenre = new byte[1];

    
//自定义的异常类
    System.Exception myException;

    
/**/
    
//////////////////////////////////////////////////////
    
/// 以下是属性,只读,获取Mp3音乐格式的题头信息
    
//////////////////////////////////////////////////////

    public string Title
    
{
        
get
        
{            
            
return Encoding.Default.GetString(sTitle);
        }

    }


    
/// <summary>
    
/// 艺术家
    
/// </summary>
    
/// <value></value>

    public string Artist
    
{
        
get
        
{
            
return Encoding.Default.GetString(sArtist);
        }

    }


    
/// <summary>
    
/// 曲目
    
/// </summary>
    
/// <value></value>

    public string Album
    
{
        
get
        
{
            
return Encoding.Default.GetString(sAlbum);
        }

    }


    
/// <summary>
    
/// 出版年月
    
/// </summary>
    
/// <value></value>

    public string Year
    
{
        
get
        
{
            
return Encoding.Default.GetString(sYear);
        }

    }


    
/// <summary>
    
/// 注释
    
/// </summary>
    
/// <value></value>

    public string Comment
    
{
        
get
        
{
            
return Encoding.Default.GetString(sComment);
        }

    }


    
/// <summary>
    
/// 获取音乐文件的分类
    
/// </summary>
    
/// <value></value>

    public string Genre
    
{
        
get
        
{
            
switch (Convert.ToInt16(sGenre[0]))
            
{
                
case 0return "Blues";
                
case 20return "Alternative";
                
case 40return "AlternRock";
                
case 60return "Top 40";
                
case 1return "Classic Rock";
                
case 21return "Ska";
                
case 41return "Bass";
                
case 61return "Christian Rap";
                
case 2return "Country";
                
case 22return "Death Metal";
                
case 42return "Soul";
                
case 62return "Pop/Funk";
                
case 3return "Dance";
                
case 23return "Pranks";
                
case 43return "Punk";
                
case 63return "Jungle";
                
case 4return "Disco";
                
case 24return "Soundtrack";
                
case 44return "Space";
                
case 64return "Native American";
                
case 5return "Funk";
                
case 25return "Euro-Techno";
                
case 45return "Meditative";
                
case 65return "Cabaret";
                
case 6return "Grunge";
                
case 26return "Ambient";
                
case 46return "Instrumental Pop";
                
case 66return "New Wave";
                
case 7return "Hip-Hop";
                
case 27return "Trip-Hop";
                
case 47return "Instrumental Rock";
                
case 67return "Psychadelic";
                
case 8return "Jazz";
                
case 28return "Vocal";
                
case 48return "Ethnic";
                
case 68return "Rave";
                
case 9return "Metal";
                
case 29return "Jazz+Funk";
                
case 49return "Gothic";
                
case 69return "Showtunes";
                
case 10return "New Age";
                
case 30return "Fusion";
                
case 50return "Darkwave";
                
case 70return "Trailer";
                
case 11return "Oldies";
                
case 31return "Trance";
                
case 51return "Techno-Industrial";
                
case 71return "Lo-Fi";
                
case 12return "Other";
                
case 32return "Classical";
                
case 52return "Electronic";
                
case 72return "Tribal";
                
case 13return "Pop";
                
case 33return "Instrumental";
                
case 53return "Pop-Folk";
                
case 73return "Acid Punk";
                
case 14return "R&B";
                
case 34return "Acid";
                
case 54return "Eurodance";
                
case 74return "Acid Jazz";
                
case 15return "Rap";
                
case 35return "House";
                
case 55return "Dream";
                
case 75return "Polka";
                
case 16return "Reggae";
                
case 36return "Game";
                
case 56return "Southern Rock";
                
case 76return "Retro";
                
case 17return "Rock";
                
case 37return "Sound Clip";
                
case 57return "Comedy";
                
case 77return "Musical";
                
case 18return "Techno";
                
case 38return "Gospel";
                
case 58return "Cult";
                
case 78return "Rock & Roll";
                
case 19return "Industrial";
                
case 39return "Noise";
                
case 59return "Gangsta";
                
case 79return "Hard Rock";
                
default:
                    
return "未知类型";
            }


        }

    }



    
public Mp3Tag()
    
{
    }


    
public Mp3Tag(byte[] Tag)
    
{        
        
        
if (Tag.Length!=128)
        
{
           
// this.myException = new Exception("不是标准的Mpeg-Mp3 Tag格式.<br>Tag长度应该是128 Byte.");
           
// throw this.myException;
            int sourceIndex = Tag.Length-128;
            Array.Copy(Tag,sourceIndex,
this.TAGBody, 0,128);
        }

        
else
        
{
            Array.Copy(Tag,
this.TAGBody,128);
        }


        
//
        Array.Copy(this.TAGBody, 3this.sTitle, 030);
        
//C
        Array.Copy(this.TAGBody, 33this.sArtist, 030);
        
//D
        Array.Copy(this.TAGBody, 63this.sAlbum, 030);
        
//E
        Array.Copy(this.TAGBody, 93this.sYear, 04);
        
//F
        Array.Copy(this.TAGBody, 97this.sComment, 030);
        
//G
        Array.Copy(this.TAGBody, 127this.sGenre, 01);

    }

}



好,我们集成测试一下.在Web Developer 2005 Express 测试通过
posted @ 2005-03-31 15:43  Slashout  阅读(908)  评论(0编辑  收藏  举报