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 convert File to base64 string based on Java

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "based on Java how to achieve file and base64 string conversion", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "based on Java how to achieve file and base64 string conversion" this article.

This article mainly introduces the implementation of file and base64 string conversion based on Java, which is introduced in great detail through the sample code, which has a certain reference value for everyone's study or work. Friends who need it can refer to it.

The project encountered the need to convert the picture to a base64-encoded string, but for extensibility, a method was written to convert the task type file. Packages to be introduced:

Commons-codec commons-codec 1.13

The source code is as follows

Import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; import java.io.*; public class Base64FileUtil {private static String targetFilePath = "E:\\ base2Img\\ target\\ test.txt"; public static void main (String [] args) throws Exception {String fileStr = getFileStr ("E:\ base2Img\\ big test.txt"); System.out.println ("fileStr = =" + fileStr); System.out.println (generateFile (fileStr, targetFilePath)); System.out.println ("end") } / * File to base64 string * convert the file to a byte array string and encode it with Base64 encoding * / public static String getFileStr (String filePath) {InputStream in = null; byte [] data = null; / / read the file byte array try {in = new FileInputStream (filePath); data = new byte [in.available ()]; in.read (data) In.close ();} catch (IOException e) {e.printStackTrace ();} finally {try {in.close ();} catch (IOException e) {e.printStackTrace ();}} / / A pair of byte array Base64 encoding BASE64Encoder encoder = new BASE64Encoder (); / / returns the byte array string return encoder.encode (data) encoded by Base64 } / * base64 strings are converted into files, which can be JPEG, PNG, TXT and AVI, etc. * * @ param base64FileStr * @ param filePath * @ return * @ throws Exception * / public static boolean generateFile (String base64FileStr, String filePath) throws Exception {/ / data is empty if (base64FileStr = = null) {System.out.println ("No, oops! "); return false;} BASE64Decoder decoder = new BASE64Decoder (); / / Base64 decoding, Base64 decoding of byte array strings and generation of the file byte [] byt = decoder.decodeBuffer (base64FileStr); for (int I = 0, len = byt.length; I < len; + + I) {/ / adjust exception data if (byt [I] < 0) {byt [I] + = 256 }} OutputStream out = null; InputStream input = new ByteArrayInputStream (byt); try {/ / generate files in the specified format out = new FileOutputStream (filePath); byte [] buff = new byte [1024]; int len = 0; while ((len = input.read (buff))! =-1) {out.write (buff, 0, len) }} catch (IOException e) {e.printStackTrace ();} finally {out.flush (); out.close ();} return true;}} these are all the contents of the article "how to convert files and base64 strings based on Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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