Base64 coded conversion binary picture __ Code

Source: Internet
Author: User
Tags base64

have been doing this lately, so the code will be a little more

Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;

Import Java.io.OutputStream;
Import Sun.misc.BASE64Decoder;

Import Sun.misc.BASE64Encoder;
public class Test64bit {public static void main (string[] args) {//test convert from BASE64 encoding to picture file String strimg = "Base64 encoding";

Generateimage (strimg, "c:\\ test. jpg");


The test converts from picture file to Base64 encoded SYSTEM.OUT.PRINTLN (GETIMAGESTR ("c:\\wangyc.jpg"));

The public static string Getimagestr (String imgfilepath) {//Converts a picture file into a byte array string and BASE64 encoding it byte[] data = null; Read the picture byte array try {InputStream in = new FileInputStream (imgfilepath); data = new byte[in.available ()]; in.read (data); in.cl
OSE ();

catch (IOException e) {e.printstacktrace ();}
BASE64 encoding the byte array Base64encoder encoder = new Base64encoder (); return Encoder.encode (data)//Returns BASE64 encoded byte array string} public static Boolean Generateimage (String imgstr, String Imgfilepa
TH) {///Base64 decoding of byte array strings and generating picture if (IMGSTR = = null)//image data is null return false; BasE64decoder decoder = new Base64decoder (); try {//Base64 decoding byte[] bytes = Decoder.decodebuffer (IMGSTR); for (int i = 0; i < bytes.length; ++i) {if (bytes[i) &L T
0) {//Adjust exception data Bytes[i] + = 256;}}
Generate JPEG picture outputstream out = new FileOutputStream (Imgfilepath);
Out.write (bytes);
Out.flush ();
Out.close ();
return true;
catch (Exception e) {return false;}} }
Transferred from blog.sina.com.cn/s/blog_81a2e91401010214.html

20140513

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.