Base64 converts files and strings)

Source: Internet
Author: User
Package com. ysj;

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;

/**
* Applicable to files in most formats
* @ Author Administrator
*
*/
Public class test {
Public static void main (string [] ARGs ){
Test T = new test ();
// String STR = T. getimagestr ("D:/My Documents/scriptxreg.zip ");
// String STR = T. getimagestr ("D:/My Documents/ip.jpg ");
String STR = T. getimagestr ("D:/My Documents/ ");
System. Out. println (STR );
// Boolean flag = T. generateimage (STR, "d:/My Documents/scriptxreg.zip ");
// Boolean flag = T. generateimage (STR, "d:/My Documents/ip.jpg ");
Boolean flag = T. generateimage (STR, "d:/My Documents/ B. B .mp3 ");
System. Out. println (FLAG );
}
/**
* Convert the file into a byte array string and encode it with base64
* @ Param imgfile
* @ Return
*/
Public static string getimagestr (string imgfile ){
Inputstream in = NULL;
Byte [] DATA = NULL;
// Read the object byte array
Try {
In = new fileinputstream (imgfile );
Data = new byte [IN. Available ()];
In. Read (data );
In. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
// Encode the byte array base64
Base64encoder encoder = new base64encoder ();
// Returns a base64 encoded byte array string.
Return encoder. encode (data );
}

/**
* Base64 Decoding Based on byte array strings and generating files
* @ Param imgstr
* @ Param savedimagepath
* @ Return
*/
Public static Boolean generateimage (string imgstr, string savedimagepath ){
// File byte array string data is empty
If (imgstr = NULL)
Return false;
Base64decoder decoder = new base64decoder ();
Try {
// Base64 Decoding
Byte [] B = decoder. decodebuffer (imgstr );
For (INT I = 0; I <B. length; ++ I ){
{// Adjust abnormal data
If (B [I] <0)
B [I] ++ = 256;
}
}
// Generate a file
// String sangimagestr = "D:/My Documents/ip.jpg"; // path of the file to be generated.
Outputstream out = new fileoutputstream (savedimagepath );
Out. Write (B );
Out. Flush ();
Out. Close ();
Return true;
} Catch (exception e ){
Return false;
}
}

}

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.