The Java-XML method transmits images.

Source: Internet
Author: User

1. Read the image to the byte [] stream

2. Transfer byte [] to base64 encoding (that is, a bunch of strings), put the strings in XML, and the images can be transmitted with XML.

3. decodes the base64 encoding in XML into a byte [] stream and then outputs it as an image.

 

Java implements base64 and byte [] mutual conversion:

Import sun. Misc. base64decoder; // convert base64 to byte []
Import sun. Misc. base64encoder; // convert byet [] to base64
Public class test {
Public static void main (string ARGs []) throws ioexception
{
// Define a base64encoder
Base64encoder encode = new base64encoder ();
// Convert byte [] to base64
String base64 = encode. encode ("five-stroke electronic computer". getbytes ());
// Output base64
System. Out. println (base64 );
// Create a base64decoder
Base64decoder decode = new base64decoder ();
// Convert base64 to byte []
Byte [] B = Decode. decodebuffer (base64 );
// Output the converted byte []
System. Out. println (new string (B ));}
}

 

// Convert the image to base64 encoding

Import java. AWT. image. bufferedimage;
Import java. Io. bytearrayoutputstream;
Import java. Io. file;
Import java. Io. ioexception;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import javax. ImageIO. ImageIO;

Import sun. Misc. base64encoder;

Public class testimagetobase64 {
 
// Instantiate the base64encoder object
Static base64encoder encoder = new sun. Misc. base64encoder ();

/*
* Get the base64 encoding of the specified image.
*/
Public static string getimagebinary (){
File F = new file ("D: // a.jpg ");
Bufferedimage Bi;
Try {
Bi = ImageIO. Read (f );
Bytearrayoutputstream baos = new bytearrayoutputstream ();
ImageIO. Write (Bi, "jpg", baos );
Byte [] bytes = baos. tobytearray ();

Return encoder. encodebuffer (bytes). Trim ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Return NULL;
}
 
/**
* @ Param ARGs
* @ Throws ioexception */
Public static void main (string [] ARGs) throws ioexception {
// Obtain the base64 encoding of the image.
String abc = getimagebinary ();
// Remove the base64 encoded line feed symbol.
Pattern P = pattern. Compile ("\ s * | \ t | \ r | \ n ");
Matcher M = P. matcher (ABC );
String after = M. replaceall ("");
// Print and remove the line feed symbol base64 encoding
System. Out. println (after );

}

}

 

 

// Restore the base64 encoded image to an image:

Import java. Io. file;

Import java. Io. fileoutputstream;

Import java. Io. ioexception;

Import sun. Misc. base64decoder;

Public class codetest {

Public static void main (string [] ARGs) throws ioexception {

// STR is the base64 encoding of an image

String STR = "examples/examples + fcgtpig8drkz1/examples + Ml7 + fovmzkleub + examples/25 m/s5tse + rtngvuy + examples/fkbopdy9/F3 + dg4o7pjebi0 + fj0 + tk5o3pz + release/V3/lu3 + release/pw3/pw4ptx4ppx4/release/r3x/release/n25/release + release/v56vj4 + release/z67pn + 3/release v366/z77p376 + v787f // ywaaaaagwbvaaai/WD/queues/eoddhmhrckrdeuxf/ilxm + chixi0bv50cf2lyx5ab + keys + A9/V/C/queues + expires + release + bdrqnlqo5b + release/release + 6u +/release/tuch/release + ADZ + release/bhb33xpu/wpl0tt/release/++ pqbb/pihgm/vstwcx/4 aagb/ b2py/3l3vxijzrloc9/0juea4l3m4kzzx + wov/9nmc + mw2pfntj 4MM + AJ/j6e6ekeyhclfiqq4fbaa7 ";

Base64decoder decoder = new base64decoder ();

Byte [] bytes = decoder. decodebuffer (STR );

File file = new file ("D:/a.jpeg ");

Fileoutputstream Fos = new fileoutputstream (File );

FOS. Write (bytes );

FOS. Flush ();

FOS. Close ();

}

1. Read the image to the byte [] stream

2. Transfer byte [] to base64 encoding (that is, a bunch of strings), put the strings in XML, and the images can be transmitted with XML.

3. decodes the base64 encoding in XML into a byte [] stream and then outputs it as an image.

 

Java implements base64 and byte [] mutual conversion:

Import sun. Misc. base64decoder; // convert base64 to byte []
Import sun. Misc. base64encoder; // convert byet [] to base64
Public class test {
Public static void main (string ARGs []) throws ioexception
{
// Define a base64encoder
Base64encoder encode = new base64encoder ();
// Convert byte [] to base64
String base64 = encode. encode ("five-stroke electronic computer". getbytes ());
// Output base64
System. Out. println (base64 );
// Create a base64decoder
Base64decoder decode = new base64decoder ();
// Convert base64 to byte []
Byte [] B = Decode. decodebuffer (base64 );
// Output the converted byte []
System. Out. println (new string (B ));}
}

 

// Convert the image to base64 encoding

Import java. AWT. image. bufferedimage;
Import java. Io. bytearrayoutputstream;
Import java. Io. file;
Import java. Io. ioexception;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import javax. ImageIO. ImageIO;

Import sun. Misc. base64encoder;

Public class testimagetobase64 {
 
// Instantiate the base64encoder object
Static base64encoder encoder = new sun. Misc. base64encoder ();

/*
* Get the base64 encoding of the specified image.
*/
Public static string getimagebinary (){
File F = new file ("D: // a.jpg ");
Bufferedimage Bi;
Try {
Bi = ImageIO. Read (f );
Bytearrayoutputstream baos = new bytearrayoutputstream ();
ImageIO. Write (Bi, "jpg", baos );
Byte [] bytes = baos. tobytearray ();

Return encoder. encodebuffer (bytes). Trim ();
} Catch (ioexception e ){
E. printstacktrace ();
}
Return NULL;
}
 
/**
* @ Param ARGs
* @ Throws ioexception */
Public static void main (string [] ARGs) throws ioexception {
// Obtain the base64 encoding of the image.
String abc = getimagebinary ();
// Remove the base64 encoded line feed symbol.
Pattern P = pattern. Compile ("\ s * | \ t | \ r | \ n ");
Matcher M = P. matcher (ABC );
String after = M. replaceall ("");
// Print and remove the line feed symbol base64 encoding
System. Out. println (after );

}

}

 

 

// Restore the base64 encoded image to an image:

Import java. Io. file;

Import java. Io. fileoutputstream;

Import java. Io. ioexception;

Import sun. Misc. base64decoder;

Public class codetest {

Public static void main (string [] ARGs) throws ioexception {

// STR is the base64 encoding of an image

String STR = "examples/examples + fcgtpig8drkz1/examples + Ml7 + fovmzkleub + examples/25 m/s5tse + rtngvuy + examples/fkbopdy9/F3 + dg4o7pjebi0 + fj0 + tk5o3pz + release/V3/lu3 + release/pw3/pw4ptx4ppx4/release/r3x/release/n25/release + release/v56vj4 + release/z67pn + 3/release v366/z77p376 + v787f // ywaaaaagwbvaaai/WD/queues/eoddhmhrckrdeuxf/ilxm + chixi0bv50cf2lyx5ab + keys + A9/V/C/queues + expires + release + bdrqnlqo5b + release/release + 6u +/release/tuch/release + ADZ + release/bhb33xpu/wpl0tt/release/++ pqbb/pihgm/vstwcx/4 aagb/ b2py/3l3vxijzrloc9/0juea4l3m4kzzx + wov/9nmc + mw2pfntj 4MM + AJ/j6e6ekeyhclfiqq4fbaa7 ";

Base64decoder decoder = new base64decoder ();

Byte [] bytes = decoder. decodebuffer (STR );

File file = new file ("D:/a.jpeg ");

Fileoutputstream Fos = new fileoutputstream (File );

FOS. Write (bytes );

FOS. Flush ();

FOS. Close ();

}

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.