Java function Library

Source: Internet
Author: User

1. File related 1.1 Determine if the directory exists
 Public Static Boolean dictionaryexist (String path) {        new  File (path);         if (File.exists () && file.isdirectory ()) {            returntrue;         Else {            returnfalse;        }    }
View Code1.2 Creating a multilevel directory
 Public Static void dictionarycreate (String path) {        new  File (path);         if (File.exists () && file.isdirectory ()) {        else  {            file.mkdirs ();        }    } 
View Code1.3 Determining whether a file exists
     Public Static Boolean fileexist (String filename) {        new  File (filename);         if (File.exists () && file.isfile ()) {            returntrue;         Else {            returnfalse;        }    }
View Code1.4 Deleting files
     Public Static void DeleteFile (String filename) {        new  File (filename);         if (File.exists () && file.isfile ()) {            file.delete ();        }    }
View Code2. HTTP-related 2.1 performing post
     Public StaticString DoPost (String uri, string json)throwsException {returnDoPost (URI, JSON,NULL); }     Public StaticString DoPost (String uri, map<string, object> params)throwsException {returnDoPost (URI, params,NULL); }     Public StaticString DoPost (String uri, String json, map<string, string> headers)throwsException {logger.debug ("Execute Http Post----------------------->>>>>>"); Logger.debug ("URI:" +URI); Logger.debug ("JSON:" +JSON); Logger.debug ("Headers:" +headers); Closeablehttpclient httpClient=Httpclients.createdefault (); HttpPost HttpPost=NewHttpPost (URI); if(Headers! =NULL) {Set<String> Headerkeys =Headers.keyset ();  for(String headername:headerkeys) {Httppost.setheader (Headername, (String) Headers.get (headername)); }        }        if(JSON! =NULL) {httppost.setentity (Newstringentity (JSON, code_type)); } closeablehttpresponse Response=Httpclient.execute (HttpPost); if(Response.getstatusline (). Getstatuscode ()! = 200) {            Throw NewException ("Error to do post:[" + response.getstatusline (). Getstatuscode () + "]" +response.getstatusline (). Getreasonphrase ()); } httpentity Entity=response.getentity (); String Responsebody=entityutils.tostring (entity, code_type);        Httpclient.close (); Logger.debug ("Responsebody:" +responsebody); Logger.debug ("Complete Http Post----------------------->>>>>>"); returnresponsebody; }     Public StaticString DoPost (String uri, map<string, object> params, map<string, string> headers)throwsException {logger.debug ("Execute Http Post----------------------->>>>>>"); Logger.debug ("URI:" +URI); Logger.debug ("Params:" +params); Logger.debug ("Headers:" +headers); Closeablehttpclient httpClient=Httpclients.createdefault (); HttpPost HttpPost=NewHttpPost (URI); if(Headers! =NULL) {Set<String> Headerkeys =Headers.keyset ();  for(String headername:headerkeys) {Httppost.setheader (Headername, (String) Headers.get (headername)); }        }        if(Params! =NULL) {List<NameValuePair> Paramspost =NewArraylist<namevaluepair>(); Set<String> Paramkeys =Params.keyset ();  for(String paramname:paramkeys) {Paramspost.add (NewBasicnamevaluepair (paramname, Params.get (paramname). toString ())); } httppost.setentity (Newurlencodedformentity (Paramspost, Code_type)); } closeablehttpresponse Response=Httpclient.execute (HttpPost); if(Response.getstatusline (). Getstatuscode ()! = 200) {            Throw NewException ("Error to do post:[" + response.getstatusline (). Getstatuscode () + "]" +response.getstatusline (). Getreasonphrase ()); } httpentity Entity=response.getentity (); String Responsebody=entityutils.tostring (entity, code_type);        Httpclient.close (); Logger.debug ("Responsebody:" +responsebody); Logger.debug ("Complete Http Post----------------------->>>>>>"); returnresponsebody; }
View Code2.2 Performing a Get
     Public StaticString doget (String uri, map<string, object> params, map<string, string> headers)throwsException {logger.debug ("Execute Http Get----------------------->>>>>>"); Logger.debug ("URI:" +URI); Logger.debug ("Params:" +params); Logger.debug ("Headers:" +headers); if(Params! =NULL) {URI= Uri + "?"; Set<String> Paramkeys =Params.keyset ();  for(String paramname:paramkeys) {URI= uri + paramname + "=" + Params.get (paramname) + "&"; } URI= uri + "guid=" +Uuid.randomuuid (). toString (); } closeablehttpclient httpClient=Httpclients.createdefault (); HttpGet HttpGet=NewHttpGet (URI); if(Headers! =NULL) {Set<String> Headerkeys =Headers.keyset ();  for(String headername:headerkeys) {Httpget.setheader (Headername, (String) Headers.get (headername)); }} closeablehttpresponse Response=Httpclient.execute (HttpGet); if(Response.getstatusline (). Getstatuscode ()! = 200) {            Throw NewException ("Error to do post:[" + response.getstatusline (). Getstatuscode () + "]" +response.getstatusline (). Getreasonphrase ()); } httpentity Entity=response.getentity (); String Responsebody=entityutils.tostring (entity, code_type);        Httpclient.close (); Logger.debug ("Responsebody:" +responsebody); Logger.debug ("Complete Http Post----------------------->>>>>>"); returnresponsebody; }
View Code3. String Correlation 3.1 Judging string is empty
     Public Static Final Boolean isEmpty (String value) {        returnnull | | value.equals ("");    }          Public Static Final Boolean isEmpty (Object value) {        returnnull | | value.tostring (). Equals ("");    
View Code3.2 Generating UUID
     Public Static Final String GetGuid () {        return uuid.randomuuid (). toString (). Replace ("-", "");    }
View Code3.3 Initial capital letters
     Public Static Final string Firstcharupper (String value) {        return value.substring (0, 1). toUpperCase () + Value.substring (1, value.length ());    }
View Code3.4 Set ID for bean
     Public Static Final<T> T Setnewid (class<t> clazz, T object, String IDfield)throwsException {IDfield=Firstcharupper (IDfield); Method GetMethod= Clazz.getmethod ("Get" +IDfield); Method Setmethod= Clazz.getmethod ("Set" +idfield, String.class); if(GetMethod = =NULL|| Setmethod = =NULL) {            returnobject; } Else{String ID=(String) Getmethod.invoke (object); if(IsEmpty (id)) {Setmethod.invoke (object, GetGuid ()); }            returnobject; }    }
View Code

Java function Library

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.