The DFS server that uploaded files in batches yesterdayProgram, And then share it in the first place. Of course, the program is messy, because some of the information I debug is left in it. It is original, and some comments are not deleted, I think these things should be able to make people understand programs faster, so they are all released. Of course, there may be comments and errors. Sorry.
Today, I completed the client program. Because I am mainly engaged in net development, I wrote a client based on my own business when DFS does not have a net client, upload files in batches by using net.CodeIt is also original and changed without debugging.
/// <Summary> /// Upload files in batches. /// </Summary> /// <Param name = "groupname"> name of the group. </param> /// <Param name = "filescount"> the files count. </param> /// <Param name = "localfilename"> name of the local file. </param> /// <Param name = "buffer"> the buffer. </param> /// <Param name = "extension"> the extension. </param> /// <Returns> </returns> Protected Static String [] dobatchupload (string groupname, ilist <byte []> filesbuffer, string [] filesextension ){ Int Filescount = filesbuffer. count; If (255 <filescount ){ If (Null! = _ Logger) {_ logger. errorformat (" The number of files uploaded in batch is {0}. The number of files uploaded in batches exceeds the limit (the number of files uploaded in batches is limited). please upload files multiple times. ", Filescount); throw new exception (" The number of uploaded files exceeds the batch processing limit! ");}} If (Null! = _ Logger) {_ logger. infoformat (" Start uploading objects in batches. The total number of objects uploaded in batches is {0 }. ", Filescount);} tcpconnection storageconnection = getstorageconnection (groupname ); If (Null! = _ Logger) _ logger. infoformat (" The storage server IP address is: {0}. The port is {1} ", Storageconnection. IPaddress, storageconnection. Port ); If (Filesbuffer. Count! = Filesextension. Length ){If (Null! = _ Logger) _ logger. errorformat (" Upload File array and upload file extension. The length of the array of the uploaded file is {0}, and the length of the array of the file extension is {1 }. ", Filesbuffer. Count, filesextension. Length); throw new exception (" The number of uploaded files does not match. ");} // Construct a transfer stream with the extension. Byte [] filesextensionbuffer = new byte [Protocol. fdfs_file_ext_name_max_len * filescount]; byte [] fileextensionbuffer = new byte [Protocol. Protocol]; byte [] filetempextensionbuffer; For ( Int I = 0; I <filesextension. length; I ++ ){If (String. isnullorempty (filesextension [I]) { If (Null! = _ Logger) _ logger. Error (" If the file extension is null, the file upload is terminated. "); Throw new exception (" No file extension is obtained. ");} Filetempextensionbuffer = encoding. getencoding (fastdfsservice. charset). getbytes (filesextension [I]); Int Fileextbufferlength = filetempextensionbuffer. length; If (Fileextbufferlength> protocol. fdfs_file_ext_name_max_len) fileextbufferlength = protocol. fdfs_file_ext_name_max_len;// Protocol Normalization Array. Copy (filetempextensionbuffer, 0, fileextensionbuffer, 0, fileextbufferlength ); // Add to Network Transmission Array. Copy (fileextensionbuffer, 0, filesextensionbuffer, I * protocol. fdfs_file_ext_name_max_len, fileextbufferlength );} // Construct a File Transfer Stream Long Filesbufferlength = 0l; foreach (byte [] filebuffer in filesbuffer) {filesbufferlength + = filebuffer. longlength ;} // Number of constructed files in byte streams Byte [] filescountbuffer = util. longtobuffer (filescount ); // Construct the header protocol Block // Protocol. tracker_proto_pkg_len_size * (filescount + 2) length of each file + Length of extended name throttling + total length of file byte streams Byte [] headerbuffer = new byte [1 + protocol. tracker_proto_pkg_len_size * (filescount + 2)]; headerbuffer [0] = (byte) storageconnection. index; // The first index is the storage index. Byte [] temp; // Each object indicates the byte stream length of each file. For ( Int I = 0; I <filesbuffer. count; I ++) {temp = util. longtobuffer (filesbuffer [I]. longlength); array. copy (temp, 0, headerbuffer, 1 + I * protocol. tracker_proto_pkg_len_size, temp. longlength);} temp = util. longtobuffer (filesextensionbuffer. longlength );// Total length of the extension Array. Copy (temp, 0, headerbuffer, 1 + filesbuffer. Count * protocol. tracker_proto_pkg_len_size, temp. longlength); temp = util. longtobuffer (filesbufferlength ); // File byte stream Length Array. Copy (temp, 0, headerbuffer, 1 + (filesbuffer. Count + 1) * protocol. tracker_proto_pkg_len_size, temp. longlength ); // Construct the Protocol transmission stream Byte [] protocalbuffer = util. packheader (protocol. storage_proto_pai_batch_upload, // Length composition: one-bit storage index + the length of each file in bytes + the total length of the file extension + the total length of the file in bytes Headerbuffer. Length + filescountbuffer. Length + filesextensionbuffer. Length + filesbufferlength, 0); _ logger. infoformat (" Number of uploaded Bytes: {0} ", Headerbuffer. length + filescountbuffer. length + filesextensionbuffer. length + filesbufferlength); stream outstream = storageconnection. getstream (); outstream. write (protocalbuffer, 0, protocalbuffer. length); outstream. write (filescountbuffer, 0, filescountbuffer. length); outstream. write (headerbuffer, 0, headerbuffer. length); outstream. write (filesextensionbuffer, 0, filesextensionbuffer. length); foreach (byte [] buffer in filesbuffer) {outstream. write (buffer, 0, buffer. length);} stream readstream; Int Filenamebufferlength = maid. tracker_proto_pkg_len_size + 128; // File Name Length + file name content Byte [] tempbuffer;Int Tempreadsize = 0; Int Filenamesize; byte [] tempfilenamebytes; Char [] Chars; Int Error; string [] filesname = new string [filescount]; For ( Int I = 0; I <filescount; I ++) {readstream = storageconnection. getstream (); tempbuffer = new byte [protocol. tracker_proto_pkg_len_size + 128]; tempreadsize = readstream. read (tempbuffer, 0, filenamebufferlength ); If (Tempreadsize! = Filenamebufferlength ){ If (Tempreadsize = 10) // An error occurs when the file is not transmitted. {Error = tempbuffer [Protocol. proto_header_status_index]; If (Null! = _ Logger) _ logger. errorformat (" An exception occurred while uploading the file. The file location is {0}. The error code is {1} ", I + 1, error); throw new exception (" File Upload error! ");} If (Null! = _ Logger) _ logger. errorformat ("An exception occurred while uploading the file. The file location is {0}. The error code cannot be returned. The error header length is {1} ", I + 1, tempreadsize); throw new exception (" File Upload error! ");} Filenamesize = ( Int ) Util. buffertolong (tempbuffer, 0); tempfilenamebytes = new byte [filenamesize]; array. copy (tempbuffer, protocol. tracker_proto_pkg_len_size, tempfilenamebytes, 0, filenamesize); chars = util. tochararray (tempfilenamebytes); filesname [I] = new string (chars, 0, filenamesize ). trim ('\ 0 '). trim ();} readstream = storageconnection. getstream (); tempbuffer = new byte [10]; tempreadsize = readstream. read (tempbuffer, 0, 10 ); If (10! = Tempreadsize ){ If (Null! = _ Logger) _ logger. errorformat (" After the file is uploaded, all file paths are returned to the client. However, a server transmission Header error occurs. Error Header Length: {0} ", Tempreadsize);} error = tempbuffer [Protocol. proto_header_status_index]; If (0! = Error ){ If (Null! = _ Logger) _ logger. errorformat (" After the file is uploaded, all file paths are returned to the client. However, an error occurs on the server. Error code: {0} ", Error );} Return Filesname ;}
When writing this program, it is used together with the server-side program for debugging. Therefore, we mainly consider the degree to which the program completes the function, not the performance, CLR-Gc, and so on. However, the performance should not be too bad, and the clr gc may be a little busy. If you are interested, you can optimize it yourself. In fact, this is not difficult.