Java file multipart upload Server Source Code

Source: Internet
Author: User

This blog introduces how to upload files in multiple parts. If you want to know more about file "multipart" download, refer to the blog "Java server support resumable source code [Support express and thunder]".
This article focuses on the server side. For details about the client side, refer to the Java file multipart upload client source code, the idea of multipart upload, and the process, I have already elaborated on this blog, and I will not go into details here.

Directly add the code. The source code of the spring MVC controller that receives HTTP multipart upload requests from the client is as follows:

@ Controllerpublic class uploadcontroller extends basecontroller {Private Static final log = logfactory. getlog (uploadcontroller. class); Private uploadservice; private authservice;/*** the large file is divided into small file blocks for upload. One part is transferred at a time. After the last part is uploaded successfully, merge all uploaded parts, save them to the corresponding location on file server *, and return the detailed attributes of the successfully uploaded files. when the last part is uploaded, the information about the successful upload is returned. In this case, use getfilelist to query the file. * The file's uploadstatus is 2. The client must handle the display of files in this status. (For UPS server) **/@ requestmapping ("/CORE/V1/file/upload") @ responsebodypublic object upload (httpservletresponse response, @ requestparam (value = "client_id ", required = false) string appkey, @ requestparam (value = "sig", required = false) string appsig, @ requestparam (value = "token", required = false) string token, @ requestparam (value = "UUID", required = false) string UUID, @ requestparam (value = "Block ", Required = false) string blockindex, @ requestparam (value =" file ", required = false) multipartfile, @ requestparam Map <string, string> parameters) {checkempty (appkey, baseexception. error_code_16002); checkempty (token, baseexception. error_code_16007); checkempty (UUID, baseexception. error_code_20016); checkempty (blockindex, baseexception. error_code_20006); checkempty (appsig, baseexception. Error_code_10010); If (multipartfile = NULL) {Throw new baseexception (baseexception. error_code_20020); // the uploaded file does not exist} Long uuidl = parselong (UUID, baseexception. error_code_20016); integer blockindexi = parseint (blockindex, baseexception. error_code_20006); Map <string, Object> appmap = getauthservice (). validatesigature (parameters); accesstoken = casutil. checkaccesstoken (token, appmap); Long UID = Accesstoken. getuid (); string bucketurl = accesstoken. getbucketurl (); // copy files from the upload directory to the working directory string fileabsulutepath = NULL; try {fileabsulutepath = This. copyfile (multipartfile. getinputstream (), multipartfile. getoriginalfilename ();} catch (ioexception IOE) {log. error (IOE. getmessage (), IOE); throw new baseexception (baseexception. error_code_20020); // the uploaded file does not exist} file uploadedfile = new file (Global. upload_temp_di R + fileabsulutepath); checkemptyfile (uploadedfile); // file non-empty verification object rs = uploadservice. upload (uuidl, blockindexi, uid, uploadedfile, bucketurl); sethttpstatusok (response); Return Rs ;} // todo check whether there is a problem here // verify private void checkemptyfile (File file) {If (file = NULL | file. getabsolutepath () = NULL) {Throw new baseexception (baseexception. error_code_20020); // the uploaded file does not exist}/*** write the file to the local folder ** @ throws Ioexception * returns the generated file name */private string copyfile (inputstream, string filename) {outputstream = NULL; string tempfilename = NULL; int pointposition = filename. lastindexof (". "); If (pointposition <0) {// myvediotempfilename = UUID. randomuuid (). tostring (); // 94d1d2e0-9aad-4dd8-a0f6-494b0099ff26} else {// myvedio. flvtempfilename = UUID. randomuuid () + filename. substring (pointpos Ition); // 94d1d2e0-9aad-4dd8-a0f6-494b0099ff26.flv} Try {outputstream = new fileoutputstream (Global. upload_temp_dir + tempfilename); int readbytes = 0; byte [] buffer = new byte [10000]; while (readbytes = inputstream. read (buffer, 0, 10000 ))! =-1) {outputstream. write (buffer, 0, readbytes);} return tempfilename;} catch (ioexception IOE) {// log. error (IOE. getmessage (), IOE); throw new baseexception (baseexception. error_code_20020); // the uploaded file does not exist} finally {If (outputstream! = NULL) {try {outputstream. Close () ;}catch (ioexception e) {}} if (inputstream! = NULL) {try {inputstream. close ();} catch (ioexception E) {}}}/*** test whether the service is available ** @ Param response * @ return * @ author zwq7978 */@ requestmapping ("/CORE/V1/file/testserver ") @ responsebodypublic object testserver (httpservletresponse response) {sethttpstatusok (response); return global. success_response;} public uploadservice getuploadservice () {return uploadservice;} public void setuploadservice (uploadservice) {This. uploadservice = uploadservice;} public void setauthservice (authservice) {This. authservice = authservice;} public authservice getauthservice () {return authservice ;}}

For example, the file to be uploaded is testjavask.mp4. The upload method uses multipartfile to receive this object, in contrast to the name defined by the block upload server for the block file parameter in Java file multipart upload client source code. For each HTTP request, use the copyfile method to output the file stream to a temporary folder on the server. For example, the author is D:/defonds/syncpath/uploadtemp.
50127019-b63b-4a54-8f53-14efd1e58ada.mp4 temporary file generation is used to save the upload file stream.

Upload parts in sequence. After all the blocks are uploaded, transfer these temporary files to the specified directory on the server. For example, the author's directory is D:/defonds/syncpath/file, in this folder, the/1/temp_dir_5_1 directory is generated, and the temporary file of uploadtemp is transferred to this folder one by one, which is generated as 5. part0001 file. The source code of file transfer is as follows:

/*** Move all blocks from the temporary file directory to the specified local directory or S2/S3 ** @ Param preupload */private void moveblockfiles (blockpreuploadfileinfo preupload) {@ suppresswarnings ("unchecked") string [] s3blockurl = new string [preupload. getblocknumber ()]; string [] localblockurl = new string [preupload. getblocknumber ()]; // The Local block file path to delete the list <blockuploadinfo> blocks = (list <blockuploadinfo>) getbasedao (). queryforlist ("upload. getblockuploadfilebyuuid ", preupload. getuuid (); string tempdirname = syncutil. gettempdirname (preupload. getuuid (), preupload. getuid (); string parentpath = Global. upload_absolute_paht _ + global. path_separative_sign + String. valueof (preupload. getuid (); string dirpath = parentpath + global. path_separative_sign + tempdirname; new file (dirpath ). mkdirs (); // create a folder for storing Block Files (local) Int J = 0; For (blockuploadinfo: blocks) {try {string strblockindex = createstrblockindex (info. getblockindex (); string suffixpath = preupload. getuuid () + ". part "+ strblockindex; string tempfilepath = info. gettempfile (); file tempfile = new file (tempfilepath); file tmpfile = new file (dirpath + suffixpath); If (tmpfile. exists () {fileutils. deletequietly (tmpfile);} fileutils. movefile (tempfile, tmpfile); localblockurl [J] = dirpath + suffixpath; j ++; info. setstatus (Global. moved_to_newdir); getbasedao (). update ("upload. updateblockupload ", Info); If (log. isinfoenabled () log.info (preupload. getuuid () + "" + info. getbuid () + "moveblockfiles");} catch (ioexception e) {log. error (E. getmessage (), e); throw new baseexception ("file not found") ;}} preupload. setlocalblockurl (localblockurl); preupload. setdirpath (dirpath); preupload. setstatus (Global. moved_to_newdir); getbasedao (). update ("upload. updatepreuploadinfo ", preupload);} private string createstrblockindex (INT blockindex) {string strblockindex; If (blockindex <10) {strblockindex =" 000 "+ blockindex ;} else if (10 <= blockindex & blockindex <100) {strblockindex = "00" + blockindex;} else if (100 <= blockindex & blockindex <1000) {strblockindex = "0" + blockindex;} else {strblockindex = "" + blockindex;} return strblockindex ;}

Finally, the Assembly source code of the file:

/***** Assembly file **/private void assemblefilewithblock (blockpreuploadfileinfo preupload) {string dirpath = preupload. getdirpath (); // start to assemble the file string uploadedurl = NULL in the specified directory; string [] separatedfiles; string [] [] separatedfilesandsize; int filenum = 0; file file = new file (dirpath); separatedfiles = file. list (); separatedfilesandsize = new string [separatedfiles. length] [2]; arrays. sort (separatedfiles); filenum = separ Atedfiles. length; For (INT I = 0; I <filenum; I ++) {separatedfilesandsize [I] [0] = separatedfiles [I]; string filename = dirpath + separatedfiles [I]; file tmpfile = new file (filename); long filesize = tmpfile. length (); separatedfilesandsize [I] [1] = string. valueof (filesize);} randomaccessfile filereader = NULL; randomaccessfile filewrite = NULL; long alreadywrite = 0; int Len = 0; byte [] Buf = new byte [1024]; Try {uploadedurl = Global. upload_absolute_paht _ + global. path_separative_sign + preupload. getuid () + global. path_separative_sign + preupload. getuuid (); filewrite = new randomaccessfile (uploadedurl, "RW"); For (INT I = 0; I <filenum; I ++) {filewrite. seek (alreadywrite); // read filereader = new randomaccessfile (dirpath + separatedfilesandsize [I] [0]), "R"); // write while (LEN = filereader. read (BUF ))! =-1) {filewrite. write (BUF, 0, Len);} filereader. close (); alreadywrite + = long. parselong (separatedfilesandsize [I] [1]);} filewrite. close (); preupload. setstatus (Global. assembled); preupload. setserverpath (uploadedurl); getbasedao (). update ("upload. updatepreuploadinfo ", preupload); If (Global. block_upload_to! = Global. block_upload_to_local) {// No problem after assembly. Delete blockstring [] Path = preupload on S2/S3. gets3blockurl (); For (string: Path) {try {If (Global. block_upload_to = Global. block_upload_to_s2) {s2util. deletefile (preupload. getbucketurl (), string);} else {s3util. deletefile (preupload. getbucketurl (), string) ;}} catch (exception e) {log. error (E. getmessage (), e) ;}}if (log. isinfoenabled () log.info (preupload. getuuid () + "Assemblefilewithblock");} catch (ioexception e) {log. Error (E. getmessage (), e); try {If (filereader! = NULL) {filereader. Close ();} If (filewrite! = NULL) {filewrite. Close () ;}catch (ioexception ex) {log. Error (E. getmessage (), e );}}}

Blockpreuploadfileinfo is our custom service file processing bean.
Okay. The multipart upload server, client source code, and workflow have all been introduced. The above source code has all been practiced by the project, and most of them are still running in some projects. If you are interested, you can modify the above Code to see if the operation is successful. If you have any questions, you can leave a comment in this blog and discuss them together.

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.