java-前端传base64,后台转成文件并存储
需求:单独的H5页面实现手绘签名,然后上传服务器,并修改数据,首先项目中是扫码跳转到H5页面,那链接中需要带参数,怎么截取参数?下面是截取路径,获得值
var varurl = window.location.search; //获取url中"?"符后的字串 if (varurl.indexOf("?") != -1) { //判断是否有参数 varstr = varurl.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串 var strs = varstr.split("="); //用等号进行分隔 (因为知道只有一个参数 所以直接用等号进分隔 如果有多个参数 要用&号分隔 再用等号进行分隔) console.log(strs[1]); //直接弹出第一个参数 (如果有多个参数 还要进行循环的) id = strs[1]; }
接下来就是后台接受字节码并转换成文件上传了:
@RequestMapping(value = "/uploadBase64", method = {RequestMethod.POST}) @ResponseBody public Object uploadBase64(HttpServletRequest request, HttpServletResponse response) throws Exception { String base64String = request.getParameter("signatureBase64"); if (StringUtils.isBlank(base64String)) { return new JsonResult(false, "上传失败", ""); } logger.info("======开始上传图片======"); String imgUrl = ""; String msg = ""; Boolean result = false; MultipartFile file = Base64DecodMultipartFileUtil.base64ToMultipart(base64String); if (Objects.nonNull(file)) { try { String path = getCompleteImgPath(request, file); imgUrl = path.replace("\\", "/"); result = true; msg = SysCode.SUCCESS.getDesc(); logger.info("======图片上传成功,路径======" + path.replace("\\", "/")); } catch (Exception e) { logger.error("图片上传失败:" + e.getMessage(), e); e.printStackTrace(); msg = "上传失败!"; } } else { logger.error("图片上传失败"); } logger.info("======图片上传结束======"); return new JsonResult(result, msg, imgUrl); }
//base64转文件
public static MultipartFile base64ToMultipart(String base64) {
   try {
      String[] baseStrs = base64.split(",");
      BASE64Decoder decoder = new BASE64Decoder();
      byte[] b = new byte[0];
      b = decoder.decodeBuffer(baseStrs[1]);
      for (int i = 0; i < b.length; ++i) {
         if (b[i] < 0) {
            b[i] += 256;
         }
      }
      return new Base64DecodMultipartFileUtil(b, baseStrs[0]);
   } catch (IOException e) {
      e.printStackTrace();
      return null;
   }
}
//获取文件路径
public static String getCompleteFilePath(HttpServletRequest request, MultipartFile uploadFile) throws Exception {
    String logoPathDir = SysConstants.TOMCAT_FILE_URL;
    /**
     * 得到文件保存目录的真实路径
     */
    String logoRealPathDir = request.getSession().getServletContext().getRealPath(logoPathDir);
    /**
     * 根据真实路径创建目录
     */
    File logoSaveFile = new File(logoRealPathDir);
    if (!logoSaveFile.exists()) {
        logoSaveFile.mkdirs();
    }
    /**
     * 获取文件的后缀
     */
    System.out.println(uploadFile.getOriginalFilename());
    String suffix = uploadFile.getOriginalFilename().substring(uploadFile.getOriginalFilename().lastIndexOf("."));
    /**
     * 使用UUID生成文件名称
     */
    String imageName = RandomUtil.getUuid() + suffix;// 构建文件名称
    /**
     * 拼成完整的文件保存路径加文件
     */
    String fileName = logoRealPathDir + File.separator + imageName;
    File file = new File(fileName);
    uploadFile.transferTo(file);
    FileChannel in = null;
    FileChannel out = null;
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = new FileInputStream(file);
        outStream = new FileOutputStream(PathUtil.getImgBasePath() + File.separator + imageName);
        in = inStream.getChannel();
        out = outStream.getChannel();
        in.transferTo(0, in.size(), out);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        inStream.close();
        in.close();
        outStream.close();
        out.close();
    }
    return PathUtil.getImgPath(imageName);
}
 
                    
                
                
            
        
浙公网安备 33010602011771号