上传用base64字符串
2022/4/19 6:13:12
本文主要是介绍上传用base64字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
@RequestMapping("/upload/image") public Object uploadFile(@RequestBody JSONObject a) throws IOException { File file = null; try { file = base64ToFile(a.getString("file")); } catch (Exception e) { e.printStackTrace(); } String url = FileUtil.uploadFile(file); return url; } public File base64ToFile(String base64) throws Exception { if(base64.contains("data:image")){ base64 = base64.substring(base64.indexOf(",")+1); } base64 = base64.toString().replace("\r\n", ""); //创建文件目录 String prefix=".jpeg"; File file = File.createTempFile(UUID.randomUUID().toString(), prefix); BufferedOutputStream bos = null; FileOutputStream fos = null; try { BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes = decoder.decodeBuffer(base64); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bytes); }finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } return file; }//上传图片 $('#createImage').on('click',function(){ let param = new FormData(); //创建form对象 let signName = document.getElementById("signName"); let twojz = $('#signName').createSignature(); let blob = dataURItoBlob($('#signName').createSignature()); console.log($('#signName').createSignature()); console.log(blob); let pmm = {"file":twojz}; let pmmStr = JSON.stringify(pmm); param.append('file', blob, "--image" + new Date().getTime() + "client_signature.png"); let pararar = {"fileStream":blob}; let par = JSON.stringify(pararar); $.ajax({ url:"http://localhost:8080/upload/image" , // 请求路径 type:"POST" , //请求方式 processData : false, contentType : "application/json", async:false, data:pmmStr, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJhZG1pbiIsImV4cCI6MTY1MDMwNzg1MH0.ciKm2SQUGbo2Olk01EnmzVnJVdON0VOgULkLP-L5OMM"); }, success:function (data) { alert(data); },//响应成功后的回调函数 error:function () { alert("出错啦...") },//表示如果请求响应出现错误,会执行的回调函数
dataType:"text"//设置接受到的响应数据的格式 }); });
}); </script>
这篇关于上传用base64字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-29DataGrip使用ssh连接数据库的操作流程
- 2024-05-28SpringBoot3.2更新声明!
- 2024-05-28中外程序员到底有啥区别?
- 2024-05-25外企也半夜发布上线吗?
- 2024-05-24鸿蒙原生应用再新丁!芒果TV 入局鸿蒙
- 2024-05-22基本概念
- 2024-05-22检索数据
- 2024-05-22排序数据
- 2024-05-22基础过滤数据
- 2024-05-22通过逻辑操作符过滤数据