远程下载帮助类
来源:互联网 发布:mysql存储过程写法 编辑:程序博客网 时间:2024/05/16 23:44
//======================================================================//// Copyright (C) 2013 仙剑// All rights reserved// guid1: beaa7167-1dd4-423a-89b1-a824572332ea// CLR Version: 4.0.30319.18063// Name: DownloadHelper// Computer: CHARLEY-PC// Organization: VoiceCyber// Namespace: VoiceCyber.Common// File Name: DownloadHelper//// created by Charley at 2014/3/22 17:55:05// http://blog.csdn.net/jian200801 ////======================================================================using System;using System.IO;using System.Net;using System.Text;namespace VoiceCyber.Common{ /// <summary> /// 文件下载帮助类,支持http和ftp下载 /// 这是一个静态类 /// </summary> public static class DownloadHelper { /// <summary> /// FTP方式下载文件 /// </summary> /// <param name="parameters"> /// 下载参数 /// 0:服务器地址 /// 1:端口 /// 2:登录名(如果为空,则匿名下载) /// 3:登录密码 /// 4:源文件路径 /// 5:存放路径 /// 6: 如果目标文件已经存在,是否替换文件(可选) /// </param> /// <returns></returns> public static OperationReturn DownloadFileFTP(string[] parameters) { OperationReturn optReturn = new OperationReturn(); optReturn.Result = true; optReturn.Code = Defines.RET_SUCCESS; try { if (parameters == null || parameters.Length < 6) { optReturn.Result = false; optReturn.Code = Defines.RET_PARAM_INVALID; optReturn.Message = string.Format("Download parameter is null or length invalid."); return optReturn; } string strTargetFile = parameters[5]; string strTargetDir = strTargetFile.Substring(0, strTargetFile.LastIndexOf("\\")); if (!Directory.Exists(strTargetDir)) { Directory.CreateDirectory(strTargetFile); } if (File.Exists(strTargetFile)) { if (parameters.Length > 6 && parameters[6] == "1") { File.Delete(strTargetFile); } else { optReturn.Result = false; optReturn.Code = Defines.RET_FILE_ALREADY_EXIST; optReturn.Message = string.Format("Target file already exist.\t{0}", strTargetFile); return optReturn; } } string strRequest = string.Empty; if (string.IsNullOrEmpty(parameters[2])) { strRequest = string.Format("ftp://{0}:{1}/{2}", parameters[0], parameters[1], parameters[4]); } else { strRequest = string.Format("ftp://{0}:{1}@{2}:{3}/{4}", parameters[2], parameters[3], parameters[0], parameters[1], parameters[4]); } FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(strRequest); WebResponse ftpWebResponse = ftpWebRequest.GetResponse(); Stream stream = ftpWebResponse.GetResponseStream(); if (stream == null) { optReturn.Result = false; optReturn.Code = Defines.RET_OBJECT_NULL; optReturn.Message = string.Format("Response stream is null"); return optReturn; } using (FileStream fileStream = new FileStream(strTargetFile, FileMode.Create, FileAccess.Write)) { byte[] buffer = new byte[1024]; int readed = stream.Read(buffer, 0, 1024); while (readed > 0) { fileStream.Write(buffer, 0, readed); readed = stream.Read(buffer, 0, 1024); } fileStream.Close(); stream.Close(); } optReturn.StringValue = strTargetFile; } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; optReturn.Exception = ex; } return optReturn; } /// <summary> /// HTTP方式下载文件 /// </summary> /// <param name="parameters"> /// 下载参数 /// 0:服务器地址 /// 1:端口 /// 2:登录名(如果为空,则匿名下载) /// 3:登录密码 /// 4:源文件路径 /// 5:存放路径 /// 6: 如果目标文件已经存在,是否替换文件(可选) /// </param> /// <returns></returns> public static OperationReturn DownloadFileHTTP(string[] parameters) { OperationReturn optReturn = new OperationReturn(); optReturn.Result = true; optReturn.Code = Defines.RET_SUCCESS; try { if (parameters == null || parameters.Length < 6) { optReturn.Result = false; optReturn.Code = Defines.RET_PARAM_INVALID; optReturn.Message = string.Format("Download parameter is null or length invalid."); return optReturn; } string strTargetFile = parameters[5]; string strTargetDir = strTargetFile.Substring(0, strTargetFile.LastIndexOf("\\")); if (!Directory.Exists(strTargetDir)) { Directory.CreateDirectory(strTargetFile); } if (File.Exists(strTargetFile)) { if (parameters.Length > 6 && parameters[6] == "1") { File.Delete(strTargetFile); } else { optReturn.Result = false; optReturn.Code = Defines.RET_FILE_ALREADY_EXIST; optReturn.Message = string.Format("Target file already exist.\t{0}", strTargetFile); return optReturn; } } string strRequest = string.Format("http://{0}:{1}/{2}", parameters[0], parameters[1], parameters[4]); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(strRequest); if (!string.IsNullOrEmpty(parameters[2])) { httpWebRequest.Credentials = new NetworkCredential(parameters[2], parameters[3]); string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", parameters[2], parameters[3]))); httpWebRequest.Headers.Add("Authorization", "Basic" + code); } WebResponse httpWebResponse = httpWebRequest.GetResponse(); Stream stream = httpWebResponse.GetResponseStream(); if (stream == null) { optReturn.Result = false; optReturn.Code = Defines.RET_OBJECT_NULL; optReturn.Message = string.Format("Response stream is null"); return optReturn; } using (FileStream fileStream = new FileStream(strTargetFile, FileMode.Create, FileAccess.Write)) { byte[] buffer = new byte[1024]; int readed = stream.Read(buffer, 0, 1024); while (readed > 0) { fileStream.Write(buffer, 0, readed); readed = stream.Read(buffer, 0, 1024); } fileStream.Close(); stream.Close(); } optReturn.StringValue = strTargetFile; } catch (Exception ex) { optReturn.Result = false; optReturn.Code = Defines.RET_FAIL; optReturn.Message = ex.Message; optReturn.Exception = ex; } return optReturn; } }}
0 0
- 远程下载帮助类
- C# 文件下载帮助类
- AS3.0 下载队列类 帮助完成Flash队列下载
- PHP 下载远程文件类
- PHP 下载远程文件类
- Downloads Help 下载帮助
- VS离线帮助下载
- openSUSE:下载帮助
- Java帮助文档下载
- msdn帮助,离线下载
- VS2012帮助下载地址
- 基于Android 下载文件时,更新UI简单帮助类
- 基于Android 下载文件时,更新UI简单帮助类
- 企业库相关帮助下载地址
- TortoiseSVN使用帮助和下载
- TortoiseSVN使用帮助和下载
- TortoiseSVN使用帮助和下载
- TortoiseSVN使用帮助和下载
- 文章姚笛甜蜜同游香港 两人婚外情曝光
- TestBooleanOperators
- 坚持,淡定,努力
- 第4周作业-数学学习工具
- 跟陌生女孩聊天
- 远程下载帮助类
- 安卓通过httpget传入json参数,请求数据
- JS与IOS交互中用到的属性
- 求N!
- mysql触发器设计时需要注意的问题
- OpenGL(13)GLU库API
- 关于设置shadowPath的重要性
- 微信图文
- Eclipse中指定DTD文件方法