上传用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-11-24Java中定时任务实现方式及源码剖析
- 2024-11-24Java中定时任务实现方式及源码剖析
- 2024-11-24鸿蒙原生开发手记:03-元服务开发全流程(开发元服务,只需要看这一篇文章)
- 2024-11-24细说敏捷:敏捷四会之每日站会
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解