最近在做一個移動端HTML5的應用,使用到了上傳功能,起初使用傳統的上傳方式上傳手機拍照的照片,由於手機拍照出來的照片一般都是好幾MB,所以上傳速度是非常慢的。
在網上找了很久找到了localResizeIMG壓縮框架,感覺非常的實用,所以在此分享給大家。
第一步:下載localResizeIMGlocalResizeIMG放在github中的,地址是:https://github.com/think2011/localResizeIMG。
第二步:在web工程中導入localResizeIMG相關js解壓localResizeIMG壓縮吧,把目錄中的dist文件夾拷貝到工程中,我的是放在js目錄下。
然後在自己的js中導入jQuery和localResizeIMG的js。如:
<span style=white-space:pre> </span><script src=<c:url value=/js/JQuery/jquery-1.10.0.min.js/>></script> <span style=white -space:pre> </span><script type=text/javascript src=<c:url value=/js/lrz/dist/lrz.bundle.js/>></script>第三步:在自己的上傳的input的file框加入onchange事件如下代碼
<input type=file id=payfile name=myfile style=display:none; onchange=fileChange(this) />
在fileChange方法中實現代碼的壓縮和對壓縮後生成的base64異步傳到後台
function fileChange(that){ var filepath=$(that).val(); if(filepath==) { return; } var extStart=filepath.lastIndexOf(.); var ext=filepath.substring(extStart,filepath.length ).toUpperCase(); if(.jpg|.png|.bmp|.jpeg.toUpperCase().indexOf(ext.toUpperCase())==-1){ alert(只允許上傳jpg、png、bmp、jpeg格式的圖片); return false; } //以圖片寬度為800進行壓縮lrz(that.files[0], { width: 800 }) .then(function (rst) { //壓縮後異步上傳$.ajax ({ url : <%=request.getContextPath()%>/common/fileUploadPicture, type: POST, data : { imgdata:rst.base64//壓縮後的base值}, dataType:json, cache:false, async: false, success : function(data) { if(data.success) { alert(data.message);///data.message為上傳成功後的文件路徑}else{ alert(data.message);///data .message為上傳失敗原因} }, error : function(){ alert(上傳失敗); } }); }); }第四步:spring mvc controller 後台接收base值並解析並保存文件import sun.misc.BASE64Decoder;//導入的base64的類/** * 文件上傳*/ @ResponseBody @RequestMapping(common/fileUploadPicture) public Object fileUploadPicture(String imgdata, HttpServletRequest request) { LOGGER.info([文件上傳( fileUploadPicture)][params:imgdata= + imgdata + ]); BASE64Decoder decoder = new BASE64Decoder(); try { String basePath = request.getRealPath(/upload_files); string imgPath=basePath+/test.jpg; // new一個文件對象用來保存圖片,默認保存當前工程根目錄File imageFile = new File(imgPath); // 創建輸出流FileOutputStream outputStream = new FileOutputStream(imageFile); // 獲得一個圖片文件流,我這裡是從flex中傳過來的byte[] result = decoder.decodeBuffer(imgdata.split(,)[1]);//解碼for (int i = 0; i < result.length; ++i) { if (result[i] < 0 ) {// 調整異常數據result[i] += 256; } } outputStream.write(result); return new Result(true, imgPath); } catch (AppException e1) { LOGGER.error([文件上傳(fileUpload) -fastdfs][errors: + e1 + ]); return new Result(false, 文件上傳失敗); } catch (Exception e) { LOGGER.error([文件上傳(fileUpload)][errors: + e + ]); return new Result(false, 文件上傳失敗); }finally{ outputStream.flush(); outputStream.close(); } }Result類:
import java.io.Serializable; public class Result implements Serializable{ private static final long serialVersionUID = 1L; private boolean success; private String message; public Result() { success = true; } public Result(boolean success, String message) { this .success = success; this.message = message; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMessage() { return message; } public void setMessage (String message) { this.message = message; } @Override public String toString() { return Result [success= + success + , message= + message + ]; } }以上就是所有步驟,希望大家多多留言指正,也希望大家多多支持VeVb武林網。