C # Processing of resumable upload and download files

Source: Internet
Author: User
Tags log4net

Class for resumable upload and download of Files written in C #

This example uses C # upload and Servlet for receiving.

C # resumable download of files as clients and servlets as servers

There is debugging failed, mail me: lijiangchxp@sina.com.cn

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 attributes
Private string filename = "";
Public String filename
{
Get {return filename ;}
Set {filename = value ;}
}

# Endregion

Private Static readonly ilog log = logmanager. getlogger (typeof (filelib ));

# Region File Upload

/// <Summary>
/// Upload a file (automatically split)
/// </Summary>
/// <Param name = "filepath"> full path name of the file to be uploaded (@ "E:/FTP/ftproot/20070228dqck.zip") </param>
/// <Param name = "hosturl"> server address </param>
/// <Param name = "bytecount"> split byte size </param>
/// <Param name = "userid"> host user id </param>
/// <Param name = "cruuent"> Current byte pointer </param>
/// <Returns> success returns ""; Failure Returns an error message </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? Metric & 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 = "the layout is uploaded successfully ";
String filename = filepath. substring (filepath. lastindexof ('//') + 1 );
Try
{

# Region resume processing
Byte [] data;
If (cruuent> 0)
{
Fstream. Seek (cruuent, seekorigin. Current );
}
# Endregion

# Region split File Upload
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 );

// Obtain the returned information
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 get File Size
/// <Summary>
/// Obtain the size of the Remote Server File in bytes
/// </Summary>
/// <Param name = "filepath"> full path name of the file to be uploaded </param>
/// <Param name = "hosturl"> server address </param>
/// <Param name = "userid"> host user id </param>
/// <Returns> Remote File Size </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); // The main system does not handle exceptions.
Try
{
Length = convert. toint64 (sremoteinfo );
}
Catch (exception Exx)
{
The length = convert. toint64 (sremoteinfo) Statement in log. Error ("filelib class getremotefilelength () is abnormal:" + Exx. Message); // We forcibly handle the exception
Length = 0;
}
GC. Collect ();

Return length;

}

/// <Summary>
/// Obtain the size of the local file in bytes
/// </Summary>
/// <Param name = "filepath"> full local file path </param>
/// <Returns> local file size in bytes </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 ("An error occurred while obtaining the local file size in the filelib class:" + ex. Message );
}
Return length;

}
# Endregion

# Region File Download
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)
{
// Offset pointer
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); // set the range value
}

Try
{
// Request from the server to obtain the Server Response Data Stream
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 ("Download" + localpath + "failed! "+" Cause: "+ ex. Message );
FS. Close ();
Result = false;
}

Return result;

}
# Endregion

}
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lijiangchxp2005/archive/2008/04/22/2315105.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.