Jodd-Java的瑞士军刀 demo

时间:2025-03-22 18:10:41
  • package ;  
  •   
  • import ;  
  • import ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  • //Jodd 是一个普通开源Java包。你可以把Jodd想象成Java的"瑞士军刀",不仅小,锋利而且包含许多便利的功能。  
  • ///doc/  
  • ///doc/quickstart/  
  • public class Demo {  
  •   
  •     public static void main(String[] args) throws Exception {  
  •   
  •         // sendemail();  
  •         testjson();  
  •         //testhttp();  
  •         //testget();  
  •         //testpage();  
  •         testconverter();  
  •         testprintf();  
  •         new Thread(new Runnable() {  
  •               
  •             public void run() {  
  •                 testwatchdir();  
  •             }  
  •         }).start();  
  •           
  •     }  
  •   
  •     private static void testwatchdir() {  
  •         DirWatcher dirWatcher = new DirWatcher("d:/java""*.txt");  
  •   
  •         (new DirWatcherListener() {  
  •             public void onChange(File file,  event) {  
  •                 (() + ":" + ());  
  •             }  
  •         });  
  •   
  •         (10000);  
  •        
  •          
  •     }  
  •   
  •     private static void testprintf() {  
  •         ("%+i"173);     // +173  
  •         ("%04d"1);      // 0001  
  •         ("%f"1.7);      // 1.700000  
  •         ("%1.1f"1.7);   // 1.7  
  •         ("%.4e"100.1e10);   // 1.0010e+012  
  •         ("%G"1.1e13);   // 1.1E+013  
  •         ("%l"true);     // true  
  •         ("%L"123);      // TRUE  
  •         ("%b"13);       // 1101  
  •         ("%,b", -13);     // 11111111 11111111 11111111 11110011  
  •         ("%#X"173);     // 0XAD  
  •         ("%,x", -1);      // ffff ffff  
  •         (("%s %s"new String[]{"one""two"}));    // one two  
  •     }  
  •   
  •     private static void testconverter() {  
  •         TypeConverter tc = (String.class);  
  •         (((123)));  
  •         (("1232"));  
  •         ((1));  
  •     }  
  •   
  •     private static void testpage() {  
  •         HttpBrowser browser = new HttpBrowser();  
  •         HttpRequest request = ("");  
  •         (request);  
  •         // process the page:  
  •         String page = ();  
  •         (page);  
  •         // create new request  
  •         HttpRequest newRequest = ("");  
  •   
  •         HttpResponse response=(newRequest);  
  •         //(());  
  •     }  
  •   
  •     private static void testhttp() {  
  •         HttpRequest request = new HttpRequest();  
  •         ("GET")  
  •             .protocol("http")  
  •             .host("")  
  •             .port(80)  
  •             .path("");  
  •         (().bodyText());  
  •     }  
  •   
  •     private static void testget() {  
  •         HttpResponse response = ("").send();  
  •   
  •         (());  
  •         // (());  
  •         // (response);  
  •         Map<String, String[]> headers = ();  
  •         for (<String, String[]> entry : ()) {  
  •             String key = ();  
  •             String[] value = ();  
  •             (key + "=");  
  •             for (String string : value) {  
  •                 (string);  
  •             }  
  •             ();  
  •         }  
  •     }  
  •   
  •     private static void testjson() {  
  •         Book book = new Book();  
  •         (120);  
  •   
  •         ("namexxx");  
  •         ("java android");  
  •         String json = new JsonSerializer().include("authors").serialize(book);  
  •   
  •         (json);  
  •         Book book2 = new JsonParser().parse(json, Book.class);  
  •         (book2);  
  •     }  
  •   
  •     // /doc/  
  •     private static void sendemail() throws Exception {  
  •         Email email = new Email();  
  •         ("infoxxx@");  
  •         ("igorxxxx@");  
  •         ("test7");  
  •   
  •         EmailMessage textMessage = new EmailMessage("Hello!",  
  •                 MimeTypes.MIME_TEXT_PLAIN);  
  •         (textMessage);  
  •   
  •         EmailMessage htmlMessage = new EmailMessage(  
  •                 "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">"  
  •                         + "<body><h1>Hey!</h1><img src='cid:'><h2>Hay!</h2></body></html>",  
  •                 MimeTypes.MIME_TEXT_HTML);  
  •         (htmlMessage);  
  •   
  •         EmailAttachment embeddedAttachment = new ByteArrayAttachment(FileUtil  
  •                 .readBytes("/"), "image/png""""");  
  •         (htmlMessage);  
  •         (embeddedAttachment);  
  •   
  •         EmailAttachment attachment = new FileAttachment(new File("/"),  
  •                 """image/jpeg");  
  •         (attachment);  
  •     }  
  • }