最近在做一个移动端html5的应用,使用到了上传功能,起初使用传统的上传方式上传手机拍照的照片,由于手机拍照出来的照片一般都是好几mb,
所以上传速度是非常慢的。
在网上找了很久找到了localresizeimg压缩框架,感觉非常的实用,所以在此分享给大家。
第一步:下载localresizeimg
localresizeimg放在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 + "]";
}
}
以上就是 使用spring mvc+localresizeimg实现html5端图片压缩上传的内容。