第一步,下載相關js文件css
https://blog-static.cnblogs.com/files/linxixinxiang/compression.js
第二步,創建jsp頁面html
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML> 3 <html> 4 <head> 5 <meta charset="utf-8"> 6 <title>測試</title> 7 <script src="plug-in/jquery-plugs/fileupload/js/jquery.1.9.1.min.js"></script> 8 <script src="plug-in/jquery-plugs/fileupload/bootstrap/js/bootstrap.min.js"></script> 9 <link href="plug-in/jquery-plugs/fileupload/bootstrap/css/bootstrap.css" type="text/css" rel="stylesheet" /> 10 <script src="my_plug/compression.js"></script> 11 <style> 12 #img { 13 width: 80px; 14 height: 30px; 15 text-align: center; 16 line-height: 30px; 17 float: left; 18 border: solid 1px #1c6a9e; 19 background-color: #25BA9A; 20 margin-top: 140px; 21 border-radius: 10px; 22 color: rgb(255, 255, 255); 23 } 24 25 #jg { 26 width: 300px; 27 height: 300px; 28 padding: 20px; 29 } 30 </style> 31 <script> 32 window.onload = function() { 33 var dd = new compression({ 34 domId: "img", // 上傳圖片的Dom 目前只支持id; 35 type: "jpg", // 壓縮後保存圖片的類型,目前支持 jpeg , png 參數:jpeg png 36 fidelity: 0.8, // 壓縮比例 (0,1] 37 imgFile: function(base64) { 40 $.ajax({ 41 url:"後臺url路徑", 42 type:"post", 43 data:{base64Data:base64}, 44 success:function(data){ 45 var d=$.parseJSON(data); 46 if(d.success){ 47 $("#imageval_id").val(d.obj); 48 $("#jg").attr("src", base64); 49 } 50 }, 51 error:function(){ 52 53 } 54 }); 56 } 57 }) 58 } 59 60 function getFilePath() { 61 return $("#imageval_id").val(); 62 } 63 </script> 64 </head> 65 66 <body> 67 <div id="img">上傳圖片</div> 68 <img id="jg" src=""> 69 <input type="hidden" id="imageval_id" /> 70 </body> 71 72 </html>
第三步,springMvc後臺代碼java
1 /** 2 * 文件上傳 3 * @param request 4 * @param response 5 * @return 6 */ 7 @RequestMapping(params = "upload", method = RequestMethod.POST) 8 @ResponseBody 9 public AjaxJson upload(@RequestParam String base64Data ,HttpServletRequest request, HttpServletResponse response) { 10 AjaxJson j = new AjaxJson(); 11 String path ="upload/"+CommonDateUtil.getNowTime("yyyyMM"); 12 String realPath = request.getSession().getServletContext().getRealPath("/") ;// 文件的硬盤真實路徑 13 String xdFilePath=path+"/"+CommonDateUtil.getNowTime("yyyyMMddHHmmssSSS")+".jpg"; 14 File fileDir = new File(realPath+ path); 15 boolean judeDirExists = FileUtilTest.judeDirExists(fileDir); 16 if(!judeDirExists){ 17 fileDir.mkdirs(); 18 } 19 String imageAllFilePath= realPath+xdFilePath; 20 boolean generateImage = Base64ImageUtil.GenerateImage(base64Data,imageAllFilePath); 21 if(generateImage){ 22 j.setSuccess(true); 23 j.setObj(xdFilePath); 24 }else{ 25 j.setSuccess(false); 26 } 27 return j; 28 }
java 根據base64文件流生成圖片代碼jquery
1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 7 import org.jeecgframework.core.testutil.CommonUtil; 8 9 import sun.misc.BASE64Decoder; 10 import sun.misc.BASE64Encoder; 11 12 public class Base64ImageUtil { 13 /** 14 * 將 base64字符串轉化爲 圖片 存儲 15 * @param imgStr base64字符串 16 * @param imgFilePath 轉化爲 圖片後的保存路徑 17 * @return 18 */ 19 public static boolean GenerateImage(String imgStr, String imgFilePath) { 20 if (imgStr == null) // 圖像數據爲空 21 return false; 22 if(imgStr.indexOf("data:image/jpeg;base64,")!=-1){ 23 imgStr=imgStr.replace("data:image/jpeg;base64,", ""); 24 } 25 BASE64Decoder decoder = new BASE64Decoder(); 26 try { 27 // Base64解碼 28 byte[] bytes = decoder.decodeBuffer(imgStr); 29 for (int i = 0; i < bytes.length; ++i) { 30 if (bytes[i] < 0) {// 調整異常數據 31 bytes[i] += 256; 32 } 33 } 34 // 生成jpeg圖片 35 OutputStream out = new FileOutputStream(imgFilePath); 36 out.write(bytes); 37 out.flush(); 38 out.close(); 39 return true; 40 } catch (Exception e) { 41 e.printStackTrace(); 42 return false; 43 } 44 } 45 /** 46 * 將圖片轉化爲 base64的字節流 47 * @param imgFilePath 圖片的存儲路徑 48 * @return 49 */ 50 public static String GetImageStr(String imgFilePath) { 51 byte[] data = null; 52 // 讀取圖片字節數組 53 try { 54 InputStream in = new FileInputStream(imgFilePath); 55 data = new byte[in.available()]; 56 in.read(data); 57 in.close(); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 // 對字節數組Base64編碼 62 BASE64Encoder encoder = new BASE64Encoder(); 63 return encoder.encode(data);// 返回Base64編碼過的字節數組字符串 64 } 65 }
以上代碼思路是:先將圖片在前臺經過 compression.js 的方法 將圖片進行壓縮爲 base64 的文件流,將base64文件流傳遞到java後臺進行接收 而後將 base64流從新轉化爲圖片,百度了半天學習許多大神的方法與思路實現了 圖片壓縮上傳功能,爲了方便之後查找記錄一下。ajax