public void download_file_test() { var param = new DownloadParameter { Url = "http://mp3.baidu.com/j?j=2&url=http%3A%2F%2Fzhangmenshiting2.baidu.com%2Fdata2%2Fmusic%2F1975289%2F1975289.mp3%3Fxcode%3Dca40cf05e05d213005efbff149ebffea", FilePath = @"c:\bad_day.mp3" }; NetworkUtility.DownloadFile(param); }
private static void DonwloadMp3(string downloadPath, string downUrl, string song, string singer) { var downDirectory = string.IsNullOrEmpty(singer) ? string.Format("{0}\\Music", downloadPath) : string.Format("{0}\\Music\\{1}", downloadPath, singer); if (!Directory.Exists(downDirectory)) Directory.CreateDirectory(downDirectory); var filePath = string.Format("{0}\\{1}.mp3", downDirectory, song); var downPara = new DownloadParameter { FilePath = filePath, Url = downUrl }; NetworkUtility.DownloadFile(downPara); }
/// <summary> /// ������Ƶ�ļ� /// </summary> public static bool DownloadFile(DownloadParameter para) { //�������ٵ�Tick Int32 privateTick = 0; //�������ݰ���С = 1KB var buffer = new byte[1024]; var request = HttpWebRequest.Create(para.Url); var response = request.GetResponse(); para.TotalLength = response.ContentLength; //�ļ����� #region ����ļ��Ƿ����ع� //���Ҫ���ص��ļ����� if (File.Exists(para.FilePath)) { var filelength = new FileInfo(para.FilePath).Length; //����ļ�������ͬ if (filelength == para.TotalLength) { //�������سɹ� return true; } } #endregion para.DoneBytes = 0; //����ֽ��� para.LastTick = Environment.TickCount; //ϵͳ���� Stream st, fs; //���������ļ��� BufferedStream bs; //������ int t, limitcount = 0; //ȷ�����峤�� if (GlobalSettings.GetSettings().CacheSizeMb > 256 || GlobalSettings.GetSettings().CacheSizeMb < 1) GlobalSettings.GetSettings().CacheSizeMb = 1; //��ȡ������ using (st = response.GetResponseStream()) { //���ļ��� using (fs = new FileStream(para.FilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 8)) { //ʹ�û����� using (bs = new BufferedStream(fs, GlobalSettings.GetSettings().CacheSizeMb*1024)) { //��ȡ��һ������ var osize = st.Read(buffer, 0, buffer.Length); //��ʼѭ�� while (osize > 0) { #region �ж��Ƿ�ȡ������ //����û���ֹ��false if (para.IsStop) { //�ر��� bs.Close(); st.Close(); fs.Close(); return false; } #endregion //����������ֽ��� para.DoneBytes += osize; //д�ļ�(����) bs.Write(buffer, 0, osize); //���� if (GlobalSettings.GetSettings().SpeedLimit > 0) { //���ؼ�����һcount++ limitcount++; //����1KB osize = st.Read(buffer, 0, buffer.Length); //�ۻ���limit KB�� if (limitcount >= GlobalSettings.GetSettings().SpeedLimit) { t = Environment.TickCount - privateTick; //����Ƿ����һ�� if (t < 1000) //���С��һ����ȴ���һ�� Thread.Sleep(1000 - t); //����count�ͼ�ʱ������������ limitcount = 0; privateTick = Environment.TickCount; } } else //��������� { osize = st.Read(buffer, 0, buffer.Length); } } //end while } //end bufferedstream } // end filestream } //end netstream //һ��˳������true return true; }
/// <summary> /// ������Ļ�ļ� /// </summary> public static bool DownloadSub(DownloadParameter para) { try { //���绺��(100KB) var buffer = new byte[102400]; var request = HttpWebRequest.Create(para.Url); var response = request.GetResponse(); //��ȡ������ var st = response.GetResponseStream(); if (!para.UseDeflate) { //���ļ��� using (var so = new FileStream(para.FilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 8)) { //��ȡ���� var osize = st.Read(buffer, 0, buffer.Length); while (osize > 0) { //д������ so.Write(buffer, 0, osize); osize = st.Read(buffer, 0, buffer.Length); } } } else { //deflate��ѹ�� var deflate = new DeflateStream(st, CompressionMode.Decompress); using (var reader = new StreamReader(deflate)) { File.WriteAllText(para.FilePath, reader.ReadToEnd()); } } //�ر��� st.Close(); //һ��˳������true return true; } catch { //��������False return false; } }
public void Execute(CommandContext context) { var parameters = context.CommandParameter.Split('|'); if (parameters.Length == 0) throw new ArgumentNullException(); var user = string.Empty; var count = 0; var sendToEmail = false; switch (parameters.Length) { case 1: user = parameters[0].Trim(); break; case 2: user = parameters[0].Trim(); count = Convert.ToInt32(parameters[1].Trim()); break; case 3: user = parameters[0].Trim(); count = Convert.ToInt32(parameters[1].Trim()); if (parameters[2].Trim() == "1") sendToEmail = true; break; } SendComment(context, string.Format("#PCң����# ��ʼΪ������Instagram���û� {0} ����Ƭ��ͼƬ�п��ܺܶ࣬�����ĵȺ���", user)); var graber = new InstagramGraber(); var images = graber.Grab(user, count); var downDirectory = string.Format("{0}\\Instagram\\{1}", context.DownloadPath, user); if (!Directory.Exists(downDirectory)) Directory.CreateDirectory(downDirectory); var downPara = new DownloadParameter(); var files = new List<string>(); foreach (var image in images) { var index = image.LastIndexOf("/"); var fileName = image.Substring(index); downPara.FilePath = string.Format("{0}\\{1}", downDirectory, fileName); downPara.Url = image; if(File.Exists(downPara.FilePath)) continue; NetworkUtility.DownloadFile(downPara); files.Add(downPara.FilePath); } var page = files.Count/100 + 1; if(sendToEmail) { for (var i = 0; i < page; i++) { var filesToSend = files.Take(100).Skip(i*100).ToArray(); SendPhotoByEmail(context, filesToSend); } } SendComment(context, string.Format("��Ƭ������ɡ�����һ��������{0}����Ƭ��", images.Count)); }