Java实现图片与Base64编码互转
程序员文章站
2024-03-12 15:24:26
淘宝里面的html用base64转换图片,不知道为什么,不过看起来好像很美好,话不多说,直接上代码:
import java.io.fileinputstream...
淘宝里面的html用base64转换图片,不知道为什么,不过看起来好像很美好,话不多说,直接上代码:
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) { // 测试从base64编码转换为图片文件 string strimg = "/9j/4aaqskzjrgabaqaaaqabaad/2wbdaaggbgcgbqghbwcjcqgkdbqndasldbksew8uhrofhh0ahbwgjc4nicisixwckdcpldaxndq0hyc5ptgypc4zndl/2wbdaqkjcqwldbgndrgyirwhmjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjiymjl/waarcadcalidasiaahebaxeb/8qahwaaaqubaqebaqeaaaaaaaaaaaecawqfbgcicqol/8qatraaagedawieawufbaqaaaf9aqidaaqrbrihmuege1fhbyjxfdkbkaeii0kxwrvs0fakm2jyggkkfhcygroljicokso0nty3odk6q0rfrkdisuptvfvwv1hzwmnkzwznaglqc3r1dnd4exqdhiwgh4ijipktljwwl5izmqkjpkwmp6ipqrkztlw2t7i5usldxmxgx8jjytlt1nxw19jz2uhi4+tl5ufo6erx8vp09fb3+pn6/8qahweaawebaqebaqebaqaaaaaaaaecawqfbgcicqol/8qatreaagecbaqdbacfbaqaaqj3aaecaxeebsexbhjbuqdhcrmimoeifekrobhbcsmzuvavynlrchyknoel8rcygromjygpkju2nzg5okneruzhselku1rvvldywvpjzgvmz2hpann0dxz3ehl6gooehyahiimkkpoulzaxmjmaoqokpaanqkmqsro0tba3ulm6wspexcbhymnk0tpu1dbx2nna4upk5ebn6onq8vp09fb3+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/kn9wucjjzuf9cldx03nrrwysohxjpjr3qoilmewfzz7iy7wdzcime1xdlsen3gxnqdn3ef0qbfegnxtzrpx5akvstphrv8atoibd9kocz5crdy3fvabgk23fntnop5vqwbxmpni/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"); // 测试从图片文件转换为base64编码 system.out.println(getimagestr("d:\\wangyc.jpg")); } public static string getimagestr(string imgfilepath) {// 将图片文件转化为字节数组字符串,并对其进行base64编码处理 byte[] data = null; // 读取图片字节数组 try { inputstream in = new fileinputstream(imgfilepath); data = new byte[in.available()]; in.read(data); in.close(); } catch (ioexception e) { e.printstacktrace(); } // 对字节数组base64编码 base64encoder encoder = new base64encoder(); return encoder.encode(data);// 返回base64编码过的字节数组字符串 } public static boolean generateimage(string imgstr, string imgfilepath) {// 对字节数组字符串进行base64解码并生成图片 if (imgstr == null) // 图像数据为空 return false; base64decoder decoder = new base64decoder(); try { // base64解码 byte[] bytes = decoder.decodebuffer(imgstr); for (int i = 0; i < bytes.length; ++i) { if (bytes[i] < 0) {// 调整异常数据 bytes[i] += 256; } } // 生成jpeg图片 outputstream out = new fileoutputstream(imgfilepath); out.write(bytes); out.flush(); out.close(); return true; } catch (exception e) { return false; } } }
以上就是本文的全部内容,了解更多java的语法,大家可以查看:《thinking in java 中文手册》、《jdk 1.7 参考手册官方英文版》、《jdk 1.6 api java 中文参考手册》、《jdk 1.5 api java 中文参考手册》,也希望大家多多支持。
上一篇: 微信第三方登录(原生)demo【必看篇】
下一篇: PHP中的函数声明与使用详解