最近在做一個(gè)移動(dòng)端HTML5的應(yīng)用,使用到了上傳功能,起初使用傳統(tǒng)的上傳方式上傳手機(jī)拍照的照片,由于手機(jī)拍照出來(lái)的照片一般都是好幾MB,所以上傳速度是非常慢的。
在網(wǎng)上找了很久找到了localResizeIMG壓縮框架,感覺(jué)非常的實(shí)用,所以在此分享給大家。
第一步:下載localResizeIMG
localResizeIMG放在github中的,地址是:https://github.com/think2011/localResizeIMG。
第二步:在web工程中導(dǎo)入localResizeIMG相關(guān)js
解壓localResizeIMG壓縮吧,把目錄中的dist文件夾拷貝到工程中,我的是放在js目錄下。
然后在自己的js中導(dǎo)入jquery和localResizeIMG的js。如:
1
2
|
< script src = "<c:url value=" /js/JQuery/jquery-1.10.0.min.js"/>"></ script > < 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方法中實(shí)現(xiàn)代碼的壓縮和對(duì)壓縮后生成的base64異步傳到后臺(tái)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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進(jìn)行壓縮 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 后臺(tái)接收base值并解析并保存文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import sun.misc.BASE64Decoder; //導(dǎo)入的base64的類(lèi) /** * 文件上傳 */ @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一個(gè)文件對(duì)象用來(lái)保存圖片,默認(rèn)保存當(dāng)前工程根目錄 File imageFile = new File(imgPath); // 創(chuàng)建輸出流 FileOutputStream outputStream = new FileOutputStream(imageFile); // 獲得一個(gè)圖片文件流,我這里是從flex中傳過(guò)來(lái)的 byte [] result = decoder.decodeBuffer(imgdata.split( "," )[ 1 ]); //解碼 for ( int i = 0 ; i < result.length; ++i) { if (result[i] < 0 ) { // 調(diào)整異常數(shù)據(jù) 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類(lèi):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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 + "]" ; } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。