Java implementation picture and BASE64 encoding mutual transfer _java

Source: Internet
Author: User
Tags base64

Taobao inside the HTML to convert the picture with Base64, do not know why, but it looks like a beautiful, not much to say, directly on the code:

Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Sun.misc.BASE64Decoder;

Import Sun.misc.BASE64Encoder; public class Base64image {public static void main (string[] args) {//test convert from BASE64 encoding to picture file String strimg = "/9j/4 aaqskzjrgabaqaaaqabaad/ 2wbdaaggbgcgbqghbwcjcqgkdbqndasldbksew8uhrofhh0ahbwgjc4nicisixwckdcpldaxndq0hyc5ptgypc4zndl/ 2wbdaqkjcqwldbgndrgyirwhmjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjl/ waarcadcalidasiaahebaxeb/8qahwaaaqubaqebaqeaaaaaaaaaaaecawqfbgcicqol/ 8qatraaagedawieawufbaqaaaf9aqidaaqrbrihmuege1fhbyjxfdkbkaeii0kxwrvs0fakm2jyggkkfhcygroljicokso0nty3odk6q0rfrkdisuptvfvwv1 hzwmnkzwznaglqc3r1dnd4exqdhiwgh4ijipktljwwl5izmqkjpkwmp6ipqrkztlw2t7i5usldxmxgx8jjytlt1nxw19jz2uhi4+ tl5ufo6erx8vp09fb3+pn6/8qahweaawebaqebaqebaqaaaaaaaaecawqfbgcicqol/ 8qatreaagecbaqdbacfbaqaaqj3aaecaxeebsexbhjbuqdhcrmimoeifekrobhbcsmzuvavynlrchyknoel8rcygromjygpkju2nZg5okneruzhselku1rvvldywvpjzgvmz2hpann0dxz3ehl6gooehyahiimkkpoulzaxmjmaoqokpaanqkmqsro0tba3ulm6wspexcbhymnk0tpu1dbx2nna4u pk5ebn6onq8vp09fb3+pn6/9oadambaairaxeapwd3+iiigaooooakkkkaciq11crwkdzzsrxxipnd64bwfifaqcsps4pten/pr/ uuaehuzfxgeo+odfuxj+07hp8arh8n/ousi71e8vp+pu8uj/l/aoeknmuafsfmj/fp9fnkx8kf7up93hh+78z/aj51p2pjxu7ty7f7xj5ccfl+ x5knl0afqdfehwendtgj/cx9x/rp3nmsef8a9s/3la9l8u7tj4xfqqphj/zzoa9zorjnp8d2eoyewkfl/wdxsenp/ albvp4l0m7cxperpkn8dvigdzopockwgaooooakkkkaciiigaooooako9jxrxmga7vmaxrnroenvexbhy1hyqnlpu9/fq6fapcxd7ewvgdy1+/ 1wa8uy8yzsvkohu+wmecn/ggbfevihtxwce6ijhkzstlucethhasr1tzns+x/zp8agfmh0z0qsw7u1tldjnghbdvjzmhnj2/kle5u/wc1/ ksrpi/u5xp3ftr1oaoxrayw0032hhlbepnbnjxsxdwsdnbn9hz5u75fnbjbxukvzei0ugeamylt2emmbzznt+ dfzc3q2du0m0ykbdvjzmhnjhb8qab1tyrue3+zz2kw3ea3of3uwnzgfrj7d91gupobvn/ cr0k90t3kisoiwpkqwxio3pfnrutxv99ldvpi3hlao5omdn+laeobjpicll/rsbz5p4+yj+lpu2qmuy/ seehvmz5p5waeubwy2om0zdy3jcnpzecfh6vik90bi7uypsrhmy3lwqem/wd16aeswz+x/aes/wdlps2+ aftoc1oq6haxxnulxbo5mvw8xlg7xn/0zwz9pvpsw/zo/n83g7cmnuonp/wrx2q6e9qoltayixg5estzj/61aho+i6zhbskrm8so8oro0h6f30f/aib9yu0sbuo8g8yppxhtlqmpwv9ctdohta5eev2mqem92/ gu38n+khuujsqotxv2/pkpd8e3+f8avigd0xts1xhmjuyemt7h5qfjigbakqutabrrrqauuuuajzrzmj8a57xjqbazomrjgzjjfkaaj/ lqbwfivxjqfinbdjm8i1bvfxxjlc785/pxfxrd9gu8ssbfkysgyofrv9fofvipwtcs6dmmg4+ tp1x7vvhgkfrda2w0nzhcp83p17e1qbtufstt7pmkgx58erjj57jpfxvs8f8ab+7c3mf3dvh3fxp9ksfypplo2h2tjg7k4b5efr/otelsi+sb/ j4/56yp9364o5y+q5k3iqafeassftsytgmjnsm81hcrqf2xyaysbrv2krjj57jpfdjjpbr29wpsobmcvtw3zc/wqn5zomk2yx2e4jdlmn8vp1z +dhohivprf/am5lug2hicaj7nrmqirb/zjp3sun65plj0b/ary+xodulywu4fd8+g5+tp1x7u5toeo3cf2f8ajux5cpzweev+c0c4chlxrq+ Fzykkymbfkhpznrzxuscux2u/w75mcm4bbxzzjnmtblwqs2v+ii2mzog+x5j7/jzspc/2y7p2xakphsn8/ptr39qocoqxdsh9ky8ytz5/xym52 +masycl7xyey77tke0bbzzxnnj9aufyph0vp2p975uflw3p165qk5tp0urmi1yaizbdfjz069vejndki4bf9rv0z3yuk3dyobnnhpp6u+ czlfto3sssry5+uwzzt9m02h/xxp+yybr8nhvn56de/ttkjk/ s3h2p5voz5e1vtr1zvkhsvhxwo9wsezswcogbbxxsc8v434v1w40nwrsi25fpjcokya/kft8pevy+ Vegb1fffabrrrqauuuuajx6v5r8srl2u7s1jlseqpylnk53f/sv6vzxhxis+stw1a6u38/y/obztx1wp/ruau4/ngoxlrthxamxiqkn5eup1p+nwszqzhr1hy7cojsdvpr2zwdc9pjqqunn+bsgn2nunn+ h610egwwv9mtph52w7d27gevgkizccxfzypbwj7uoybst5h+bn171qrqp/aggjecnn/ppdz09kvledy4c+ztjzt6z696uxqx/bm/ n5n6dk5jpknzyl4wh2iljxgbz8vprwdpzyywmqiulujow8w/ mmfxvxsyqwz20gff5rxnpwd9hgs1t4z5jqm7emfxqxmo9jkb11juvvvuvgm8jhy+n61chtpfiygdvorz5h461fntyftdft+/ axomy6f4u5ileom48zysj7mpeoazjnklesgrlkedgrvpzc/r6vs+wxhnxbnypbvtq7z8npb9sv1kwq74sb8/ w9pwq8lre88339xvt3tp3xqlkovs2ulwy3g8thmz88yhgmdm1zulqztw5fyoavdy+yfn5/xnaslpbx2mhjk8l3+ fpnokjkso7a4t3iejxbvcomyqds1fohszjpybm21g6u3ifsj7v8w5tngkdsuj5z6bhl6pf5v+s844xjpn+ Lwtcjtftvxivnbvlff0xjvj3rktzlh+zopthk+d/s7t23+wk2gc0zs/eme1ks7qketcvmh5+eohfnew+d783/aict2azjje+ qspyk8xl2n2yz8zzt+xplxjgm8zr0j4zyrjt7qcnz5fmeyqsy3/pvkhoflrrqauuuuaffffaeb/dbmvbttkuozlu02mhzzepkj9ofx/pxvz+ lejem9purfvzyvuybdpojtdkv/d8cf59agdnld5pl+inziibbmqreefj0z9ek7lw60smmsfrvizxhrgrn8k5ocgao+ jlxiouwzjepjpy9cfxmup8ipk0nwhu0ljxhlklbaizcdogr+vh+5unni29ksqw+0y8sbf72pb1pjrp5aatahnj3damwnvtgd4x/dz7elzhslew8t8xahjjb1qafkit/aeye85g3pv0qn5lfvatxg7ulqtg/lopnapotu61rondw804jbgpvbevfqxq+ 0p5c9rxsq40beysjabjpu6cvd5t7t++gcjnduanjljkn7pr6nb05qji2+xzbjghb2/eq5hbs7j+9bx1+brztziwjsb5bgg4g7pteuxv/ iz5k53fd2+3xfnkt+6/cqrgzo37ts7g8hyxk53fe3+3smkn5kx70aygru+9sa5dxwftdx/oyyebypl/fpofxncx5k/ 2hp2cpf52pl8k4xjrj9m10+uqy/brgvcrisntq+yfk9/bfc55c/wdz+pt8e/zc+z5xxjhtp64rph8bxz+ mu75xntf9dqsutc3lh5oeqd2xxppw2mjaddb4iof3nybu25rzjum8+1kxibqibl80/pzyqo+a9v+ hnvpfolvo4cvjwvbdvkhzilpkwgaooooakkkkae5rkvfpholwmfwxg5ernh+ e1dbx61g6h0daapajawqapemybzcjgcbcbp8acul8gq2w0isehzdhxnfjnrxlrlc3z3jgjdizvvshkfl6/pwx4pjc+f4pgt0jz5h+qjj+ p0rgfvwnutkmm1buuisnusxac7nxgfxqjfrrpqbjiqvj+g3pxqxluue320mvxjbnfmzbpc7/acxj+/ t56etbtygvbekpvsv5lkbc4zuyz5pbmtqpubezy1kpg5x0qgzy3tpnkcg8fl5j5/dvukbp5ueivq85xb0ogazunmno7djnhtpvaaddh+/ 5er6vjk7mxgcmdcdekrtm/ Kn9wucjjzuf9cldx03nrrwysohxjpjr3qoilmewfzz7iy7wdzcime1xdlsen3gxnqdn3ef0qbfegnxtzrpx5akvstphrv8atoibd9kocz5crdy3fvabgk23fn Tnop5vqwbxmpni/mbv29p1oh1jlupcr4k3f6qrdv6dcvoxeuyfux0gtjpqm5fxbawndctnzmpgwbgonffcm/2gttp+xjyfo/2d27b/acxxqmuqm+j32ilp7qxa2/e9vfncjpnncajhggs4lbzf3igigyx1x/ wrgyte0ns4nq1exitzvomsfommds17fpthbpljfz2w2xj9wvxujwgtveunuldbh5f3tv3snau9zwoaue+yicoqdrsclqyaooooakkkkae/ cmsfcnp5ooefpqb51pqjc8k8rgrubfgm/wb0vn4shft4lsghvhsd9wzj/ wvqlyi8tinrixv8hpv9k3robypd1naacerhxmuy9ct8rm6pdncw0ccewj68881vttliju7o3klnlmjjhl9+spitl0xyo+ djnhnmdb9r6hd659qcdz3spdd/bc3mk4sfh2fvlylg4wfpxwwtln9ghe7r5keeef3nnwnh2s+wry4z83v9kuifuto+ djnhnkycniwvar2or06fhuf1ejel+ydr6+9togezudnhr7vacv73g9r3+tbotjhlsnturkd5ingj/ wcwfp7ytjrhi8kbtyvrlbknazlgyn54yfuv10ik7xchtjn3pi0issbaochpnhoz8nouj9j+3jz52ek3mbs8/ jxvyapbjdhuhcqmc81bj2ywvya43evttzehmrchobjmomwfzej7s8j3d95er9k5twndb6fpg15fwv5m5aom4+lddbj/aejg9dnmskoxgj0c/ k0ao7yy3c5x9kskcpfl0qc+j9nkrx86ay55rvj24rhtb2z6hp8ja5td5fp+ xxchtv0tdffgkkwkflw5zbrrrqauuuuajxrxzrz6uc56uacvqfrmmrve65t3+donyvct/tp9kxfap9f9ztwrqeni5k3c/wcsjziss0tvk0+ 5do6ayeys8rjxxnyhyp88csjjkrf73fpsvfj5vx5kjcp5adfxzg461ywpozu59me1bzwctijz+926uysab5dv3ud3slkkew/ 7zjjjx0qrdckih3k4gchb1ogxr5qodv3ccdpsone42n1zx0p73mo5ozw2orj2qcsw1kjjeengcjym361ailjed1xpt+lp61bcfy3pj5aylwoo8cjjyog29eavwmu+td84orjpvgwnkmz/ags+ 3smp5m5cdon3sl3j5x3+m9cvwkvpojo/kaygotadt8qcz+3lnb069qzile4li3zyb13fc46vqpbg4jngr/3kkzjphsmw6hci38h1yc537vl/ kjkfcokwtdt3a485l/douxfrxshhfvrs2s0tehqcayqx2fbqhynhunzshclpbs1zauuuuaffffacc+ thoethfhgaadhfutsqpahiypsrngka6bk20acoypge+tjnaz0pzyl9oxt+b1z7uxx2mhoz73oeljvk83/ pnxodhgwty3pl8dgrnrwdf29vpbbgi3ljna3eyqesw7w+r83g3p+nmevoye8yb5+ d3skmqr6dbc22opaqchgppfnmsbz0ar4fmqj5tx96uf2gqsfiw6jjg7b6f41cnro0+ysm7pd8ufckya3ecnfilgenzhjmr8blvk+ tka5oetz4ui3vhwzn38xt1qagwbc+x+xb29pwogwdyetnzxu6zpsy74/k5igdnps/vvl/anvpt0pv+0eamombu6fjuclunbxv+ fwrnjz1romvjauj+fizfdxpwzxxta45i9jzrjpzscynlxxvkc0ugpaaciiigaooooatmjvr+nhfrqadqoao3wigda1ybqrkzbuptmszcv2no/ 5v7upauquoknt3srpmgolcxlkix23z8397pt6vjm2hmpxtlhdwsqlw+ gxkop45rkk0o2wgfzbqrgu7eutzk1sjmckpewixji3deazcij7wmrdltna3eyqdqhurf2zaid3m+gknk46flvabsdp+ Zmv2qtawbjyfeplicuxlgpltp+bern5fsorsh902isdcon596xobkbsmt/d9m08bdzky2/kefmjrusdpgszz2stksg3db05qw3eanfilgenzdoacw3ytdy+qpyd3wpmww8p9lx5ny7uu2pvzn8fz84gbjrwb50cdvu8r5d+nu7virtiypdwv7vkmphpsghnvwnqlaa7h87nlmrfejsoao/ wuw4g7gj0o7hmj05oawiiigaooooakkkkae49p0o4z0/sjmjnnabxjp+lhgen6uc4o5oap393hy2m90+nkkb+ek4y63fbm+ b5ptd7elavjduii7d7hk7bzrvfb/cfc3zxuu9yy2d/tuhyp6dkzqm1ew1vzesvzxbixg7wcc/xirbz+ xdhh8hnj3jms6ac0e3ma8zb8u7pnrxis+fzbbq4ez1+ bbjgevoa5zc6oeqo7aphsha3y7elv4dkkbzk2hi53j396wjtuwo8kvvn3hstjgmbf8khbvovjy5k27hn51znb/wdr1yhvldfpf8456/ ooef1qos92stfmmbwx8w4/wrnib7zjlfhnzonucf3j1/grlpbg91cshzd21t2cy684qalasxe9vv3/adez13jpj1zwrbk9u8vx98rkhkbv9z/ jvgfipsvhmbn/tnokrareqrywlovmebof24x9zpgakyp/aeorphlejpyrfmts8z6fpxneg9bt5rt7llkfkjkkl5n8f0rpec12hghgdx+ lhhhh6uc4nhpfaakwiigaooooakkkwr7xrywciwxnz5gih7ugdc/gsi+8qwvgmnzp3/uw67643uven1cytbruneckcng+vfc1zq/ abpv36hhncfmep56cf59avka7c58y6pjclhbwmcmbgtli2/hgf51yd34p1e6sys3fxplwi28uzrjmnnryf++kryxueu3knftgu35j+x0/ ws2ylfapi4rmpau3dcunbz3pbnbbutsylo3higqouuk+xwvpp2qdz54dw8xif2f3tnl8ev6uy6837pcbdisvowmhabn171htf+0c+euz/ Nlv56en60ajbtrs3vnjitsfpgf8sjp4uyyspderbitzlsn3axbss4trnmlunm+xcpmjc8+vatdj/pgieo8qhnlb+g59e9cc4ch30585hcw/+kvi0rhtpzlmk/l0z+lvxanym/0cpo4fl5xxr/ n8a15fd52imvrj7u7gopsoxbs+wr9pxorz5h461bovyo5n8obzb0yfl+7yfy9avxq/mtfufawchz97/gmmkrhi/wbixjgrv+9z+ touukk375smha3fd/woenmuza2lynbgsjfcce3xfyiuli5vipprvs8iqwyx8xhum9su3ujp75nkv3gi0k++jdjgomacfk863/0laaq7l8z7/ V75rpow+2c1af2czijbmo7gjt0dmrewsn339/xnj4d8ba1p9ki3dv56itaszqj5cduamxgt7tczuuikthd/ 3p8adfyewag7u7z71bhnc743mp4gpu5/xfbhme1az4htnuvfutdmv3ew46e1bxpzxhxnz2d5bi97gaykfjk5c56gd81p6drgqwy+ zbqiz28j5myxykoqs9jorhtk8dweww6p5ykbi8+e5xjh+ndnbnhcrisf0enujoagcaiiigdyrvpet/ffff8ad/8apoosfbvzlql95/gp51n/ agism7zhtcomwkezrcg01+zpd3w7zd219+cyxnnfrl7pyerppzubhtocdp8asq8eou8jsio3y3bozr9eal+3hysjb2+ptzsakuqpcrovm+ Ztgm4xjb/hwvpp2bnzjg9xs+tfu2568yqxd6i6aoseaydds5h61qj1jylzrbwwzeoefb/wgdeujlx7lb7vo2fntxjpxnnsfup7v/5aed+ g37v59kpg/avqpt7cgdau6frs/wboyb9/kwb/ao9t5/pnaesx2t7jcbfo2fluzjpxjfmas3tirdx5+z5tidgevoaux7bsot7ca9rs6/ Ruzv2khtb25a6dzwp1qj0+civoq6flq1e4ka8zftocyx93/cpbcw3ln/rdu4z6z71zy1sajjmseby9ts5/nvxnapl48idhpsrjqq5d0kc+c1zjb+zbisxpg3p6nr+nz99qnuzrm3h83zqjf+ntviqnzrhbcrwqeyonydkrwxr5mojtxll1itr9eaundnm6k+qsjhar2hl/ vvl832znh8qnf7n59rnzd21nmmyxnjnuvt7bdv2e325zjzxn86kooospmegv4b2dppzxzy4yyn2fz7rhm7tr784xjpok57xabkoc1un8/ warecmy3btv8svqf2g4leqqzbqdnx681s1a6avi0+zwxxodpj4z69agc7i1vbozk+bleqwdomem1ut5lsnjfk+042fnnb03dp+ lz2lau8d4sbt7bkgcihoghpzwrndmjybbw3lng/u+v60aq3c8olbyd+d7ffxnof9k3dh1q7srvtz9jnsa5ee5m7kvlxxhcnea464/ wqacty4460a9j/4wdef8+kh/ajeorhftf+fmoqpzgz8klwjp9x5dv/8alpvj/nnvii4/mjqxuauuarv/akusyspy5k00qpd0aw7wtzleot/ pnviqole/y4/+ulwop3ske7mga/5z0f8alsij/lpqqrysr29v5k8nlxx/6yssz+047it/aesot/pnwdrnxjcajlbicxrhcl2aptr+60+ evehpu1fqun7hv2txh5kkcckckkf+sq3xbzs0en7slfpxv6bez31uwug3soh70hmjr/1zo/5avjh0kpetbedv7up95hhviq2b9o/ 7z0azn9h9nui7ip8a5zyvsezhcwlzd1v3twpky7uarf8alop4p9xjuwbnb2q1afvbf5+f3dacexh/am9kktakkap/
    2xd3d2lknwnutwv4ogfgcefwceyrzgl2vkcwuk5lqt09 ";
    
    Generateimage (strimg, "d:\\wangyc.jpg");
  The test converts from picture file to Base64 encoded SYSTEM.OUT.PRINTLN (GETIMAGESTR ("d:\\wangyc.jpg")); } PublIC static string Getimagestr (String imgfilepath) {//Converts a picture file into a byte array string and BASE64 encoding it byte[] data = null;
      Reads the picture byte array try {InputStream in = new FileInputStream (Imgfilepath);
      data = new byte[in.available ()];
      In.read (data);
    In.close ();
    catch (IOException e) {e.printstacktrace ();
    ///To byte array Base64 encoding Base64encoder encoder = new Base64encoder (); return Encoder.encode (data)//Returns BASE64 encoded byte array string} public static Boolean Generateimage (String imgstr, String Imgfil
    Epath) {//Base64 decoding of byte array string and generate picture if (imgstr = = null)//image data is null return false;
    Base64decoder decoder = new Base64decoder ();
      try {//Base64 decode byte[] bytes = Decoder.decodebuffer (IMGSTR);
        for (int i = 0; i < bytes.length; ++i) {if (bytes[i) < 0) {//Adjust exception data bytes[i] = 256;
      }///generate JPEG picture outputstream out = new FileOutputStream (Imgfilepath);
      Out.write (bytes); OUt.flush ();
      Out.close ();
    return true;
    catch (Exception e) {return false;

 }
  }
}

Above is the entire contents of this article, learn more Java syntax, you can see: "Thinking in Java Chinese manual", "JDK 1.7 reference manual in English", "JDK 1.6 API Java Chinese Reference manual", "JDK 1.5 API Java Chinese reference manual, but also hope that we support the cloud-dwelling community.

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.