Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to receive uploaded base64 pictures in springboot

Shulou Source: shulou.com Published: 2022-06-02 11:08:41 10月04日 Update

This article shows you how to receive uploaded base64 pictures in springboot. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

@ ApiOperation (value = "update user information", notes = "update user information, including mobile phone number, mailbox, avatar, etc.", httpMethod = "GET") @ PostMapping ("/ profile") public ApiResult profile (@ RequestBody SysUserVo sysUserVo) {Boolean result = false; String imageRelativePath = "static/upload"; String imageBase64Str; Integer userId = UserUtil.getUserId (request) / / if the same if (userId > 0) {/ / Save avatar StringBuffer fileName = new StringBuffer (); fileName.append (UUID.randomUUID (). ToString (). ReplaceAll ("-", ")); if (StringUtils.isBlank (sysUserVo.getAvatar () {return new ApiResult (false) } else if (sysUserVo.getAvatar (). IndexOf (_ "data:image/png;")! =-1) {imageBase64Str = sysUserVo.getAvatar (). Replace ("data:image/png;base64,", "); fileName.append (" .png ");} else if (sysUserVo.getAvatar (). IndexOf (_" data:image/jpeg) ")! =-1) {imageBase64Str = sysUserVo.getAvatar () .replace (" data:image/jpeg;base64, ","); fileName.append (".jpeg");} else {return new ApiResult (false);} File path Try {path = new File (ResourceUtils.getURL ("classpath:"). GetPath (); if (! path.exists ()) {path = new File ("");} File uploadPath = new File (path.getAbsolutePath (), imageRelativePath) If (! uploadPath.exists ()) {uploadPath.mkdirs ();} String relativeFile = imageRelativePath+ "/" + fileName.toString (); File file = new File (uploadPath, fileName.toString ()); byte [] fileBytes = Base64.getDecoder () .decode (imageBase64Str); FileUtils.writeByteArrayToFile (file, fileBytes) SysUserVo.setUserId (userId); sysUserVo.setAvatar (relativeFile);} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace (); return new ApiResult (false) } / / Update user information result = sysUserService.updateInfoByUserId (sysUserVo);} return new ApiResult (result);} the above content is how to receive uploaded base64 images in springboot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Information users updates pictures content avatars skills knowledge same concise concise that is mobile phones mobile phone numbers articles more articles industries information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Huawei MariaDB OPPO Reno