Upload4j secure, efficient, and easy-to-use Java HTTP file upload framework

Source: Internet
Author: User
Tags html form http file upload

Brief introduction

UPLOAD4J is a lightweight HTTP file upload framework, easy to use, efficient, functional, and get rid of the traditional HTTP file upload framework cumbersome.

Upload4j was born not to address all of the upload requirements, but to focus on the basic general requirements.

Upload4j brought the

     · The implementation of the direct file stream upload, HTML form stream upload two upload mode.

· The IO operation part uses the NIO mechanism.

· File filtering is supported and filtered based on the binary header, rather than the traditional file name extension filter.

· The upload file size is not limited by memory size.

Upload4j can't give.

· File upload progress.

· Bulk upload.

Upload4j Use Example

Direct file stream upload

Enable Filter function

1 //Filter Filters2MetaFilter MetaFilter =NewMetaFilter ();3Metafilter.add (". jpg", "ffd8ff");//allowable file types, params: file suffix name, file header hex string4map<string, string> metamap =NewHashmap<string, string>();5Metamap.put (". png", "89504E47");6Metafilter.frommap (METAMAP);//add allowed file types in bulk7 8 //Configuration9Metafilterconfig metafilterconfig = Metafilterconfig.custom (). Setbuffersize (8192)//default 8192B, Unit BTen. setmaxfilesize (1024 * 1024)//limit file maximum 1M, Unit B One. SetFilter (MetaFilter)//Filter Filters A. Setrequest (Request)//reads a file stream from the body of the request object -. Setfilenamewithoutsuffix ("123")//save file name (without extension, auto-identify) -. Setsavepath ("/home/user1/upload/img/2015/09/24/");//Save Path the  - //Upload -Streamupload streamupload =Newstreamupload (); -Simpleuploadresponse response =streamupload.upload (metafilterconfig); +  - //Upload Results + if(Response.issuccess ()) {//Success A     //get File Save full path at Response.getfilepath (); -     // ...... -}Else{//failed -     //determine if failure is controllable -     if(response.getexception () = =NULL){//controllable -         //get the reason for failure in response.geterrmsg (); -         // ...... to}Else{//not controllable +         //throwing Exceptions directly -         Throwresponse.getexception (); the     } *}

Do not enable the filtering function

1 //Configuration2Streamconfig streamconfig = Streamconfig.custom (). Setbuffersize (8192)//default 8192B, Unit B3. setmaxfilesize (1024 * 1024)//limit file maximum 1M, Unit B4. Setrequest (Request)//reads a file stream from the body of the request object5. Setfilenamewithsuffix ("123.jpg")//Save full file name (with extension)6. Setsavepath ("/home/user1/upload/img/2015/09/24/");//Save Path7 8 //Upload9Streamupload streamupload =Newstreamupload ();TenSimpleuploadresponse response =streamupload.upload (streamconfig); One  A //Upload Results - if(Response.issuccess ()) {//Success -     //get File Save full path the Response.getfilepath (); -     // ...... -}Else{//failed -     //determine if failure is controllable +     if(response.getexception () = =NULL){//controllable -         //get the reason for failure + response.geterrmsg (); A         // ...... at}Else{//not controllable -         //throwing Exceptions directly -         Throwresponse.getexception (); -     } -}

HTML form stream upload

Enable Filter function

1 //Filter Filters2MetaFilter MetaFilter =NewMetaFilter ();3Metafilter.add (". jpg", "ffd8ff");//allowable file types, params: file suffix name, file header hex string4map<string, string> metamap =NewHashmap<string, string>();5Metamap.put (". png", "89504E47");6Metafilter.frommap (METAMAP);//add allowed file types in bulk7 8 //Configuration9Metafilterconfig metafilterconfig = Metafilterconfig.custom (). Setbuffersize (8192)//default 8192B, Unit BTen. setmaxfilesize (1024 * 1024)//limit file maximum 1M, Unit B One. SetFilter (MetaFilter)//Filter Filters A. Setrequest (Request)//reads a file stream from the body of the request object -. Setfilenamewithoutsuffix ("123")//save file name (without extension, auto-identify) -. Setsavepath ("/home/user1/upload/img/2015/09/24/");//Save Path the  - //Upload -Formupload formupload =Newformupload (); -Simpleuploadresponse response =formupload.upload (metafilterconfig); +  - //Upload Results + if(Response.issuccess ()) {//Success A     //get File Save full path at Response.getfilepath (); -     // ...... -}Else{//failed -     //determine if failure is controllable -     if(response.getexception () = =NULL){//controllable -         //get the reason for failure in response.geterrmsg (); -         // ...... to}Else{//not controllable +         //throwing Exceptions directly -         Throwresponse.getexception (); the     } *}

Do not enable the filtering function

1 //Configuration2Formconfig formconfig = Formconfig.custom (). Setbuffersize (8192)//default 8192B, Unit B3. setmaxfilesize (1024 * 1024)//limit file maximum 1M, Unit B4. Setrequest (Request)//reads a file stream from the body of the request object5. Setfilenamewithoutsuffix ("123")//save file name (without extension, auto extract)6. Setsavepath ("/home/user1/upload/img/2015/09/24/");//Save Path7 8 //Upload9Formupload formupload =Newformupload ();TenSimpleuploadresponse response =formupload.upload (formconfig); One  A //Upload Results - if(Response.issuccess ()) {//Success -     //get File Save full path the Response.getfilepath (); -     // ...... -}Else{//failed -     //determine if failure is controllable +     if(response.getexception () = =NULL){//controllable -         //get the reason for failure + response.geterrmsg (); A         // ...... at}Else{//not controllable -         //throwing Exceptions directly -         Throwresponse.getexception (); -     } -}

Browse UPLOAD4J Projects

Upload4j secure, efficient, and easy-to-use Java HTTP file upload framework

Related Article

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.