C# 断点续传 上传、下载文件处理
2008-04-22 15:21
501 查看
用 C# 写的 文件断点续传、下载的类
本用例是 采用 C# 上传、Servlet 进行接收
C#作为客户端、Servlet 作为服务器进行文件断点下载
有调试不通过的,给我 Mail:lijiangchxp@sina.com.cn
我写了一个简单的 例子,http://download.csdn.net/detail/lijiangchxp2005/2585179 可以下载完整的解决方案
using System;
using log4net;
using System.Collections;
using System.Text;
using System.IO;
using System.Net;
using log4net.Config;
using Chxp.Business;
namespace Chxp.Service
{
public class FileLib
{
#region 属性
private string fileName = "";
public string FileName
{
get { return fileName; }
set { fileName = value; }
}
#endregion
private static readonly ILog LOG = LogManager.GetLogger(typeof(FileLib));
#region 文件上传
/// <summary>
/// 上传文件(自动分割)
/// </summary>
/// <param name="filePath">待上传的文件全路径名称(@"E:/FTP/ftproot/20070228DQCK.zip")</param>
/// <param name="hostURL">服务器的地址</param>
/// <param name="byteCount">分割的字节大小</param>
/// <param name="userID">主机用户ID</param>
/// <param name="cruuent">当前字节指针</param>
/// <returns>成功返回"";失败则返回错误信息</returns>
public string UpLoadFile(string filePath, string hostURL, int byteCount,string userID,long cruuent)
{
string tmpURL = hostURL;
byteCount = byteCount * 1024;
//http://localhost:8080/fism/app?service=fileupload&beanId=com.cfcc.fism.service.upload.CollFileSaveServiceImpl&action=upload&filename=AI1215900000020051130411.zip&userid=test&npos=333
//action=length
System.Net.WebClient WebClientObj = new System.Net.WebClient();
FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader bReader = new BinaryReader(fStream);
long length = fStream.Length;
string sMsg = "版式上传成功";
string fileName = filePath.Substring(filePath.LastIndexOf('//') + 1);
try
{
#region 续传处理
byte[] data;
if (cruuent > 0)
{
fStream.Seek(cruuent, SeekOrigin.Current);
}
#endregion
#region 分割文件上传
for (; cruuent <= length; cruuent = cruuent + byteCount)
{
if (cruuent + byteCount > length)
{
data = new byte[Convert.ToInt64((length - cruuent))];
bReader.Read(data, 0, Convert.ToInt32((length - cruuent)));
}
else
{
data = new byte[byteCount];
bReader.Read(data, 0, byteCount);
}
try
{
LOG.Debug(data);
//***
hostURL = tmpURL + "&action=upload" + "&filename=" + fileName + "&userid=" + userID + "&npos=" + cruuent.ToString();
byte[] byRemoteInfo = WebClientObj.UploadData(hostURL, "POST", data);
string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo);
// 获取返回信息
if (sRemoteInfo.Trim() != "")
{
sMsg = sRemoteInfo;
break;
}
}
catch (Exception ex)
{
sMsg = ex.ToString();
break;
}
#endregion
}
}
catch (Exception ex)
{
sMsg = sMsg + ex.ToString();
}
try
{
bReader.Close();
fStream.Close();
}
catch (Exception exMsg)
{
sMsg = exMsg.ToString();
}
GC.Collect();
return sMsg;
}
#endregion
#region 获取文件大小
/// <summary>
/// 获取远程服务器文件字节大小
/// </summary>
/// <param name="filePath">待上传的文件全路径名称</param>
/// <param name="hostURL">服务器的地址</param>
/// <param name="userID">主机用户ID</param>
/// <returns>远程文件大小</returns>
public long GetRemoteFileLength(string filePath, string hostURL, string userID)
{
long length = 0;
System.Net.WebClient WebClientObj = new System.Net.WebClient();
string fileName = filePath.Substring(filePath.LastIndexOf('//') + 1);
hostURL = hostURL +"&action=length" + "&filename=" + fileName + "&userid=" + userID + "&npos=0" ;
byte[] data = new byte[0];
byte[] byRemoteInfo = WebClientObj.UploadData(hostURL , "POST", data);
string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo);//主系统没有作异常处理
try
{
length = Convert.ToInt64(sRemoteInfo);
}
catch (Exception exx)
{
LOG.Error("FileLib类GetRemoteFileLength()中length = Convert.ToInt64(sRemoteInfo)语句异常:" + exx.Message);//我们强制处理异常
length = 0;
}
GC.Collect();
return length;
}
/// <summary>
/// 获得本地文件字节大小
/// </summary>
/// <param name="filePath">本地文件全路径</param>
/// <returns>本地文件字节大小</returns>
public long GetLocalFileLength(string filePath)
{
long length = 0;
try
{
string fileName = filePath.Substring(filePath.LastIndexOf('//') + 1);
FileStream s = new FileStream(filePath, FileMode.Open);
length = s.Length;
s.Close();
}
catch(Exception ex)
{
LOG.Error("FileLib类中获取本地文件大小异常:"+ex.Message);
}
return length;
}
#endregion
#region 文件下载
public bool DownLoadFile(string localPath, string hostURL, int byteCount, string userID, long cruuent)
{
bool result = true;
string tmpURL = hostURL;
byteCount = byteCount * 1024;
hostURL = tmpURL + "&npos=" + cruuent.ToString();
System.IO.FileStream fs;
fs = new FileStream(localPath, FileMode.OpenOrCreate);
if (cruuent > 0)
{
//偏移指针
fs.Seek(cruuent, System.IO.SeekOrigin.Current);
}
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(hostURL);
if (cruuent > 0)
{
request.AddRange(Convert.ToInt32(cruuent)); //设置Range值
}
try
{
//向服务器请求,获得服务器回应数据流
System.IO.Stream ns = request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[byteCount];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, byteCount);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = ns.Read(nbytes, 0, byteCount);
}
fs.Close();
ns.Close();
}
catch(Exception ex)
{
LOG.Error("下载" + localPath + "的时候失败!" + "原因是:" + ex.Message);
fs.Close();
result = false;
}
return result;
}
#endregion
}
}
本用例是 采用 C# 上传、Servlet 进行接收
C#作为客户端、Servlet 作为服务器进行文件断点下载
有调试不通过的,给我 Mail:lijiangchxp@sina.com.cn
我写了一个简单的 例子,http://download.csdn.net/detail/lijiangchxp2005/2585179 可以下载完整的解决方案
using System;
using log4net;
using System.Collections;
using System.Text;
using System.IO;
using System.Net;
using log4net.Config;
using Chxp.Business;
namespace Chxp.Service
{
public class FileLib
{
#region 属性
private string fileName = "";
public string FileName
{
get { return fileName; }
set { fileName = value; }
}
#endregion
private static readonly ILog LOG = LogManager.GetLogger(typeof(FileLib));
#region 文件上传
/// <summary>
/// 上传文件(自动分割)
/// </summary>
/// <param name="filePath">待上传的文件全路径名称(@"E:/FTP/ftproot/20070228DQCK.zip")</param>
/// <param name="hostURL">服务器的地址</param>
/// <param name="byteCount">分割的字节大小</param>
/// <param name="userID">主机用户ID</param>
/// <param name="cruuent">当前字节指针</param>
/// <returns>成功返回"";失败则返回错误信息</returns>
public string UpLoadFile(string filePath, string hostURL, int byteCount,string userID,long cruuent)
{
string tmpURL = hostURL;
byteCount = byteCount * 1024;
//http://localhost:8080/fism/app?service=fileupload&beanId=com.cfcc.fism.service.upload.CollFileSaveServiceImpl&action=upload&filename=AI1215900000020051130411.zip&userid=test&npos=333
//action=length
System.Net.WebClient WebClientObj = new System.Net.WebClient();
FileStream fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader bReader = new BinaryReader(fStream);
long length = fStream.Length;
string sMsg = "版式上传成功";
string fileName = filePath.Substring(filePath.LastIndexOf('//') + 1);
try
{
#region 续传处理
byte[] data;
if (cruuent > 0)
{
fStream.Seek(cruuent, SeekOrigin.Current);
}
#endregion
#region 分割文件上传
for (; cruuent <= length; cruuent = cruuent + byteCount)
{
if (cruuent + byteCount > length)
{
data = new byte[Convert.ToInt64((length - cruuent))];
bReader.Read(data, 0, Convert.ToInt32((length - cruuent)));
}
else
{
data = new byte[byteCount];
bReader.Read(data, 0, byteCount);
}
try
{
LOG.Debug(data);
//***
hostURL = tmpURL + "&action=upload" + "&filename=" + fileName + "&userid=" + userID + "&npos=" + cruuent.ToString();
byte[] byRemoteInfo = WebClientObj.UploadData(hostURL, "POST", data);
string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo);
// 获取返回信息
if (sRemoteInfo.Trim() != "")
{
sMsg = sRemoteInfo;
break;
}
}
catch (Exception ex)
{
sMsg = ex.ToString();
break;
}
#endregion
}
}
catch (Exception ex)
{
sMsg = sMsg + ex.ToString();
}
try
{
bReader.Close();
fStream.Close();
}
catch (Exception exMsg)
{
sMsg = exMsg.ToString();
}
GC.Collect();
return sMsg;
}
#endregion
#region 获取文件大小
/// <summary>
/// 获取远程服务器文件字节大小
/// </summary>
/// <param name="filePath">待上传的文件全路径名称</param>
/// <param name="hostURL">服务器的地址</param>
/// <param name="userID">主机用户ID</param>
/// <returns>远程文件大小</returns>
public long GetRemoteFileLength(string filePath, string hostURL, string userID)
{
long length = 0;
System.Net.WebClient WebClientObj = new System.Net.WebClient();
string fileName = filePath.Substring(filePath.LastIndexOf('//') + 1);
hostURL = hostURL +"&action=length" + "&filename=" + fileName + "&userid=" + userID + "&npos=0" ;
byte[] data = new byte[0];
byte[] byRemoteInfo = WebClientObj.UploadData(hostURL , "POST", data);
string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo);//主系统没有作异常处理
try
{
length = Convert.ToInt64(sRemoteInfo);
}
catch (Exception exx)
{
LOG.Error("FileLib类GetRemoteFileLength()中length = Convert.ToInt64(sRemoteInfo)语句异常:" + exx.Message);//我们强制处理异常
length = 0;
}
GC.Collect();
return length;
}
/// <summary>
/// 获得本地文件字节大小
/// </summary>
/// <param name="filePath">本地文件全路径</param>
/// <returns>本地文件字节大小</returns>
public long GetLocalFileLength(string filePath)
{
long length = 0;
try
{
string fileName = filePath.Substring(filePath.LastIndexOf('//') + 1);
FileStream s = new FileStream(filePath, FileMode.Open);
length = s.Length;
s.Close();
}
catch(Exception ex)
{
LOG.Error("FileLib类中获取本地文件大小异常:"+ex.Message);
}
return length;
}
#endregion
#region 文件下载
public bool DownLoadFile(string localPath, string hostURL, int byteCount, string userID, long cruuent)
{
bool result = true;
string tmpURL = hostURL;
byteCount = byteCount * 1024;
hostURL = tmpURL + "&npos=" + cruuent.ToString();
System.IO.FileStream fs;
fs = new FileStream(localPath, FileMode.OpenOrCreate);
if (cruuent > 0)
{
//偏移指针
fs.Seek(cruuent, System.IO.SeekOrigin.Current);
}
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(hostURL);
if (cruuent > 0)
{
request.AddRange(Convert.ToInt32(cruuent)); //设置Range值
}
try
{
//向服务器请求,获得服务器回应数据流
System.IO.Stream ns = request.GetResponse().GetResponseStream();
byte[] nbytes = new byte[byteCount];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, byteCount);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = ns.Read(nbytes, 0, byteCount);
}
fs.Close();
ns.Close();
}
catch(Exception ex)
{
LOG.Error("下载" + localPath + "的时候失败!" + "原因是:" + ex.Message);
fs.Close();
result = false;
}
return result;
}
#endregion
}
}
相关文章推荐
- C# 断点续传 上传、下载文件处理
- C# 断点续传 上传、下载文件处理
- AIR文件上传与文件断点续传方式下载
- C#下载大文件并实现断点续传
- iOS开发-使用NSURLSession实现文件断点下载,文件离线续传以及图片上传
- C#实现上传文件分割,断点续传上传文件
- C# 文件下载之断点续传实现代码
- C#实现上传文件分割,断点续传上传文件
- C# 断点续传 上传、下载文件处理
- C# 文件下载断点续传
- C# 文件下载之断点续传
- C# 断点续传 上传、下载文件处理
- C# 文件下载之断点续传
- C# HttpWebRequest可断点上传,下载文件;SOCKET多线程多管道可断点传送大文件
- 用C#上传下载文件
- .net 实现上传文件分割,断点续传上传文件
- C#实现文件上传下载
- Atitit..文件上传组件选型and最佳实践总结(3)----断点续传控件的实现
- C#.net文件批量上传解决方案附下载(swfupload)