[1.1]用WebService返回歌曲的曲目信息.借鉴[星集工作室 张麟 Dephi版]
Dephin源文请看 http://editblog.csdn.net/csdn/archive/2004/09/07/710.aspx 谢谢 张麟 同志的blog
.Net 版本 Web Service建立一个服务类代码
然后我们再添加一个存储了Mp3Tag题头的类 Mp3Tag.Class
好,我们集成测试一下.在Web Developer 2005 Express 测试通过
.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;
}
}
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 0: return "Blues";
case 20: return "Alternative";
case 40: return "AlternRock";
case 60: return "Top 40";
case 1: return "Classic Rock";
case 21: return "Ska";
case 41: return "Bass";
case 61: return "Christian Rap";
case 2: return "Country";
case 22: return "Death Metal";
case 42: return "Soul";
case 62: return "Pop/Funk";
case 3: return "Dance";
case 23: return "Pranks";
case 43: return "Punk";
case 63: return "Jungle";
case 4: return "Disco";
case 24: return "Soundtrack";
case 44: return "Space";
case 64: return "Native American";
case 5: return "Funk";
case 25: return "Euro-Techno";
case 45: return "Meditative";
case 65: return "Cabaret";
case 6: return "Grunge";
case 26: return "Ambient";
case 46: return "Instrumental Pop";
case 66: return "New Wave";
case 7: return "Hip-Hop";
case 27: return "Trip-Hop";
case 47: return "Instrumental Rock";
case 67: return "Psychadelic";
case 8: return "Jazz";
case 28: return "Vocal";
case 48: return "Ethnic";
case 68: return "Rave";
case 9: return "Metal";
case 29: return "Jazz+Funk";
case 49: return "Gothic";
case 69: return "Showtunes";
case 10: return "New Age";
case 30: return "Fusion";
case 50: return "Darkwave";
case 70: return "Trailer";
case 11: return "Oldies";
case 31: return "Trance";
case 51: return "Techno-Industrial";
case 71: return "Lo-Fi";
case 12: return "Other";
case 32: return "Classical";
case 52: return "Electronic";
case 72: return "Tribal";
case 13: return "Pop";
case 33: return "Instrumental";
case 53: return "Pop-Folk";
case 73: return "Acid Punk";
case 14: return "R&B";
case 34: return "Acid";
case 54: return "Eurodance";
case 74: return "Acid Jazz";
case 15: return "Rap";
case 35: return "House";
case 55: return "Dream";
case 75: return "Polka";
case 16: return "Reggae";
case 36: return "Game";
case 56: return "Southern Rock";
case 76: return "Retro";
case 17: return "Rock";
case 37: return "Sound Clip";
case 57: return "Comedy";
case 77: return "Musical";
case 18: return "Techno";
case 38: return "Gospel";
case 58: return "Cult";
case 78: return "Rock & Roll";
case 19: return "Industrial";
case 39: return "Noise";
case 59: return "Gangsta";
case 79: return "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);
}
//B
Array.Copy(this.TAGBody, 3, this.sTitle, 0, 30);
//C
Array.Copy(this.TAGBody, 33, this.sArtist, 0, 30);
//D
Array.Copy(this.TAGBody, 63, this.sAlbum, 0, 30);
//E
Array.Copy(this.TAGBody, 93, this.sYear, 0, 4);
//F
Array.Copy(this.TAGBody, 97, this.sComment, 0, 30);
//G
Array.Copy(this.TAGBody, 127, this.sGenre, 0, 1);
}
}
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 0: return "Blues";
case 20: return "Alternative";
case 40: return "AlternRock";
case 60: return "Top 40";
case 1: return "Classic Rock";
case 21: return "Ska";
case 41: return "Bass";
case 61: return "Christian Rap";
case 2: return "Country";
case 22: return "Death Metal";
case 42: return "Soul";
case 62: return "Pop/Funk";
case 3: return "Dance";
case 23: return "Pranks";
case 43: return "Punk";
case 63: return "Jungle";
case 4: return "Disco";
case 24: return "Soundtrack";
case 44: return "Space";
case 64: return "Native American";
case 5: return "Funk";
case 25: return "Euro-Techno";
case 45: return "Meditative";
case 65: return "Cabaret";
case 6: return "Grunge";
case 26: return "Ambient";
case 46: return "Instrumental Pop";
case 66: return "New Wave";
case 7: return "Hip-Hop";
case 27: return "Trip-Hop";
case 47: return "Instrumental Rock";
case 67: return "Psychadelic";
case 8: return "Jazz";
case 28: return "Vocal";
case 48: return "Ethnic";
case 68: return "Rave";
case 9: return "Metal";
case 29: return "Jazz+Funk";
case 49: return "Gothic";
case 69: return "Showtunes";
case 10: return "New Age";
case 30: return "Fusion";
case 50: return "Darkwave";
case 70: return "Trailer";
case 11: return "Oldies";
case 31: return "Trance";
case 51: return "Techno-Industrial";
case 71: return "Lo-Fi";
case 12: return "Other";
case 32: return "Classical";
case 52: return "Electronic";
case 72: return "Tribal";
case 13: return "Pop";
case 33: return "Instrumental";
case 53: return "Pop-Folk";
case 73: return "Acid Punk";
case 14: return "R&B";
case 34: return "Acid";
case 54: return "Eurodance";
case 74: return "Acid Jazz";
case 15: return "Rap";
case 35: return "House";
case 55: return "Dream";
case 75: return "Polka";
case 16: return "Reggae";
case 36: return "Game";
case 56: return "Southern Rock";
case 76: return "Retro";
case 17: return "Rock";
case 37: return "Sound Clip";
case 57: return "Comedy";
case 77: return "Musical";
case 18: return "Techno";
case 38: return "Gospel";
case 58: return "Cult";
case 78: return "Rock & Roll";
case 19: return "Industrial";
case 39: return "Noise";
case 59: return "Gangsta";
case 79: return "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);
}
//B
Array.Copy(this.TAGBody, 3, this.sTitle, 0, 30);
//C
Array.Copy(this.TAGBody, 33, this.sArtist, 0, 30);
//D
Array.Copy(this.TAGBody, 63, this.sAlbum, 0, 30);
//E
Array.Copy(this.TAGBody, 93, this.sYear, 0, 4);
//F
Array.Copy(this.TAGBody, 97, this.sComment, 0, 30);
//G
Array.Copy(this.TAGBody, 127, this.sGenre, 0, 1);
}
}
好,我们集成测试一下.在Web Developer 2005 Express 测试通过
本文来自博客园,作者:Slashout,转载请注明原文链接:https://www.cnblogs.com/SlashOut/archive/2005/03/31/129321.html