首页前端开发HTMLspring mvc+localResizeIMG实现H5端图片压缩上传

spring mvc+localResizeIMG实现H5端图片压缩上传

时间2024-01-23 07:13:03发布访客分类HTML浏览191
导读:收集整理的这篇文章主要介绍了spring mvc+localResizeIMG实现H5端图片压缩上传,觉得挺不错的,现在分享给大家,也给大家做个参考。这次给大家带来sPRing mvc+localResizeimg实现H5端图片压缩上传,s...
收集整理的这篇文章主要介绍了spring mvc+localResizeIMG实现H5端图片压缩上传,觉得挺不错的,现在分享给大家,也给大家做个参考。这次给大家带来sPRing mvc+localResizeimg实现H5端图片压缩上传,spring mvc+localResizeiMG实现H5端图片压缩上传的注意事项有哪些,下面就是实战案例,一起来看一下。

最近在做一个移动端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 + "]";
      }
    }
    

相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!

推荐阅读:

分页查询的使用详解

Node.js如何开发微信墙

以上就是spring mvc+localResizeIMG实现H5端图片压缩上传的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

spring

若转载请注明出处: spring mvc+localResizeIMG实现H5端图片压缩上传
本文地址: https://pptw.com/jishu/583891.html
Canvas实现波浪进度图(附demo) canvas与h5如何实现视频截图功能

游客 回复需填写必要信息