Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement background processing with java to restore base64 pictures to files

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian introduces in detail for you "java how to achieve background processing base64 picture restore to file", detailed content, clear steps, details handled properly, hope that this "java how to achieve background processing base64 picture restore to file" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge bar.

Background processing base64 image back to file / * parse base64 image to local * @ param imgStr * @ return local temporary file address * / private static String generateImage (String imgStr) {if (Strings.isNullOrEmpty (imgStr)) {return null;} BASE64Decoder decoder = new BASE64Decoder (); / / convert front-end data imgStr = imgStr.replaceAll ("", "+") / / remove the redundant imgStr=imgStr.replace (_ "data:image/png;base64,", "); try {/ / Base64 decode byte [] b = decoder.decodeBuffer (imgStr); for (int I = 0; I < b. Resume; iDead +) {if (b [I] < 0) {/ / adjust exception data b [I] + = 256 }} String filepath = System.getProperty ("java.io.tmpdir") + "Test" + System.currentTimeMillis () + ".png"; File file = new File (filepath); if (file.exists ()) {file.delete ();} FileOutputStream fos = new FileOutputStream (file); fos.write (b); fos.flush () Fos.close (); logger.info ("path" + filepath); return filepath;} catch (Exception e) {return null;}} / / imgStr=imgStr.replace (_ "data:image/png;base64,", "); filter the corresponding type java Base64 parsing according to the image type in key places

Recently, in the business scenario, the characters passed by the third party need to be decrypted by base64, and the data is parsed according to the parsing tool provided by the third party document.

The parsing method for Base64 is as follows: String sign = "xxxxxxxxxxxxxxxxxxxxxxxx"; sun.misc.BASE64Decoder decode = new sun.misc.BASE64Decoder (); String json = new String (decode.decodeBuffer (sign))

Use sun.misc.BASE64Decoder to parse the data, and test the test environment to find that the parsed string is correct.

But after going online, according to the sign passed by the third party, after parsing, we found that there was an extra character "7" in the string, and the query logic found no problem. Finally, we guessed that there was something wrong with sun.misc.BASE64Decoder, so we changed the parsing jira of Base64.

Parse String sign = "xxxxxxxxxxxxxxxxxxxxxxxxx"; Base64 base64 = new Base64 (); String json = new String (base64.decodeBase64 (sign.getBytes () using the following code

It is found that the data returned in json is normal, and the problem is solved.

Read this, the "java how to achieve background processing base64 pictures back to file" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report