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

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

.Net 版本 Web Service建立一个服务类代码
None.gif <% @ WebService Language = " C# "  Class = " GetBinaryFile "   %>
None.gif
None.gif
using  System.Web;
None.gif
using  System.Web.Services;
None.gif
using  System.Web.Services.Protocols;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Diagnostics;
None.gif
using  System.Web.UI;
None.gif
using  System.IO;
None.gif
None.gif
None.gif
None.gif[WebService(Namespace 
=   " http://localhost/WebMp3Downloads/ " , Description  =   " 在WebServices里利用Dot.Net框架进行文件传输 " )]
ExpandedBlockStart.gifContractedBlock.gif
public   class  GetBinaryFile  : System.Web.Services.WebService  dot.gif {
InBlock.gif
InBlock.gif    [WebMethod]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string HelloWorld() dot.gif{
InBlock.gif        
return "Hello World";
ExpandedSubBlockEnd.gif    }

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

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return getBinaryFile("D:\\" + requestFileName);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 将文件转化成2进制流 ConvertFileStreamToByteBuffer
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="filename"></param>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

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

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new byte[0];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return new byte[0];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

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

InBlock.gif    public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int b1;
InBlock.gif        System.IO.MemoryStream tempStream 
= new System.IO.MemoryStream();
InBlock.gif
InBlock.gif        
while ((b1 = theStream.ReadByte()) != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tempStream.WriteByte(((
byte)b1));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return tempStream.ToArray();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 返回传送文件的文件类型 Get File Type of the Specific File
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

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

InBlock.gif
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 返回传送文件的题头信息的类,以SOAP返回Tag类
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <returns></returns>

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

InBlock.gif    
ExpandedBlockEnd.gif}

None.gif
None.gif


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

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

None.gif public   class  Mp3Tag
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private byte[] TAGBody = new byte[128];
InBlock.gif    
private byte[] sTag = new byte[3];
InBlock.gif    
private byte[] sTitle = new byte[30];
InBlock.gif    
private byte[] sArtist = new byte[30];
InBlock.gif    
private byte[] sAlbum = new byte[30];
InBlock.gif    
private byte[] sYear = new byte[4];
InBlock.gif    
private byte[] sComment = new byte[30];
InBlock.gif    
private byte[] sGenre = new byte[1];
InBlock.gif
InBlock.gif    
//自定义的异常类
InBlock.gif
    System.Exception myException;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**///
InBlock.gif    
/// 以下是属性,只读,获取Mp3音乐格式的题头信息
ExpandedSubBlockEnd.gif    
//

InBlock.gif    public string Title
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{            
InBlock.gif            
return Encoding.Default.GetString(sTitle);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 艺术家
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <value></value>

InBlock.gif    public string Artist
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Encoding.Default.GetString(sArtist);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 曲目
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <value></value>

InBlock.gif    public string Album
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Encoding.Default.GetString(sAlbum);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 出版年月
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <value></value>

InBlock.gif    public string Year
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Encoding.Default.GetString(sYear);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 注释
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <value></value>

InBlock.gif    public string Comment
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Encoding.Default.GetString(sComment);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 获取音乐文件的分类
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <value></value>

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

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    
public Mp3Tag()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

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

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Array.Copy(Tag,
this.TAGBody,128);
ExpandedSubBlockEnd.gif        }

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

ExpandedBlockEnd.gif}

None.gif


好,我们集成测试一下.在Web Developer 2005 Express 测试通过
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值