zoukankan      html  css  js  c++  java
  • 前台页面上传data image图片,java后台接收图片保存

    最近在项目中有这么一个需求,就是上传一个视频文件,然后要获取视频文件的第一帧图片,这个可以通过canvas获取得到,得到的是一个dataURL,之后还要将这个图片上传到云,这个时候如何操作就不清楚了,于是乎,google一番,总结如下:

    1. 将dataURL转成Blob
    2. 利用formData
    3. 异步上传
    function b64toBlob(b64Data, contentType='', sliceSize=512) {
            const byteCharacters = atob(b64Data);
            const byteArrays = [];
    
            for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
                const slice = byteCharacters.slice(offset, offset + sliceSize);
    
                const byteNumbers = new Array(slice.length);
                for (let i = 0; i < slice.length; i++) {
                    byteNumbers[i] = slice.charCodeAt(i);
                }
    
                const byteArray = new Uint8Array(byteNumbers);
    
                byteArrays.push(byteArray);
            }
    
          const blob = new Blob(byteArrays, {type: contentType});
          return blob;
    }

    // dataURL to blob
    
    // 假设一个dataURL
    const ImageURL = "转成Base64的变量";
    
    // Split the base64 string in data and contentType
    const block = ImageURL.split(";");
    
    // Get the content type of the image
    const contentType = block[0].split(":")[1];// In this case "image/jpeg"
    
    // get the real base64 content of the file
    const realData = block[1].split(",")[1];// In this case "R0lGODlhPQBEAPeoAJosM...."
    
    // Convert it to a blob to upload
    var blob = b64toBlob(realData, contentType);
    // new a formData
    
    const formData = new FormData();
    formData.append('blob', blob);
    // upload
    
    $.ajax({
        url:url,
        data: formData
        type:"POST",
        contentType:false,
        processData:false,
        error:function(err){
        },
        success:function(data){
        },
    });

     后台接受代码

    @RequestMapping(value = "/uploadImage")
    @ResponseBody
    public ApiMessage uploadImage(MultipartFile file, HttpServletRequest request) {
      try {
        //自定义处理图片保存方法     
    return ApiMessage.succeed(Utils.getImageUrl2(file));   } catch (Exception e) {     return ApiMessage.error();   } }
  • 相关阅读:
    最短路必经点(边)
    [HNOI2008]越狱
    【模版】卢卡斯定理
    偶数
    [USACO17FEB]Why Did the Cow Cross the Road I S
    [USACO17FEB]Why Did the Cow Cross the Road II S
    [USACO07NOV]电话线Telephone Wire
    [JSOI2007]祖码Zuma
    单人纸牌_NOI导刊2011提高(04)
    [USACO13OPEN]重力异常
  • 原文地址:https://www.cnblogs.com/MrSong97/p/9523666.html
Copyright © 2011-2022 走看看