front end:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>简单的html5 File测试 for pic2base64</title> <style> </style> <script> window.onload = function () { varINPUT = document.getElementById ( " fielinput " ); var txshow = document.getElementById ( " txshow " ); IF ( typeof (the FileReader) === ' undefined ' ) { result.innerHTML = " Sorry, your browser does not support FileReader, use modern browsers operation! " ; input.setAttribute ( ' Disabled ' , ' Disabled ' ); } the else { input.addEventListener ( ' Change ' , readFile, to false ); txshow.onclick = function () {input.click ();} } } function readFile () { var File = the this .files [ 0 ]; // determine whether it is picture type / * IF (/ image \ / \ W + / Test (file.type)!.) { Alert ( "Photo select only"); return to false; } * / var Reader = new new the FileReader (); reader.readAsDataURL (file); reader.onload = function (e) { txshow.src = this.result; document.getElementById("data").innerText=this.result.substring(this.result.indexOf(',')+1); } } </script> </head> <body> <input type="file" id="fielinput" /> <img id="txshow" style="width:100px;height:100px;"/> <br/>解析之后的base64数据:<br/> <p id="data"></p> </body> </html>
Backstage:
// BASE64 decoded file into File public UapAttachs toolsBase64ToFile (Base64 String, String fileName) { UapAttachs uapAttachs = null ; IF (! = Base64 null ! && base64.isEmpty ()) { String FILEPATH; // file path String FILENAME; // filename String FileExt = ""; // file suffix Long FILESIZE; // file size String emcrypt; // encrypted file name // FILESIZE = (file.getSize () / 1048576); = FILESIZE (base64.length () - (base64.length () / 8L) * 2L) / 1048576 ; / * limits the size of 20MB or less * / IF (FILESIZE> 20 is ) { return null ; } / * Create attachment folder * / emcrypt = String.valueOf (System.currentTimeMillis ()); file Attachment = new new file ( "Attachment" ); IF (! {attachment.exists ()) attachment.mkdir (); } / * create a local file * / FILENAME = fileName!=null?fileName:""; if (FILENAME.indexOf(".") >= 0) { FILEEXT = FILENAME.substring(FILENAME.lastIndexOf(".")+1,FILENAME.length()); } File targetfile = new File(attachment.getAbsolutePath(),emcrypt+"_"+FILENAME); /* 解码base64到文件对象 */ BufferedOutputStream bos = null; java.io.FileOutputStream fos = null; try { byte[] bytes = Base64.getDecoder().decode(base64); fos = new java.io.FileOutputStream(targetfile); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } The catch );(IOException E) { e.printStackTrace (); } } IF (fos =! Null ) { the try { fos.close (); } the catch (IOException E) { e.printStackTrace (); } } } / * return file object information * / FILEPATH = targetfile.getPath (). Replace ( "\\", "/" uapAttachs = new new UapAttachs(); if (targetfile.exists()) { uapAttachs.setFilepath(FILEPATH); uapAttachs.setFilename(FILENAME); uapAttachs.setFileext(FILEEXT); uapAttachs.setFilesize(FILESIZE); uapAttachs.setAttachtype(""); uapAttachs.setCnname(""); uapAttachs.setObjvalue(""); uapAttachs.setObjid(3529L); } } return uapAttachs; }