Front-end upload picture to back end

Source: Internet
Author: User
Tags base64

Question: The front end uploads the picture, the backend how to handle can obtain the picture the URL.

Method One, the front-end through the control to the back end of the string is Base64 encoded, the back end to get this string after the Base64 decoding, the decoded picture saved to a server location, and then the database store pictures in the server path. (The following code is from: http://www.cnblogs.com/libra0920/p/5754356.html)

/**
 * @Description: Convert base64 encoded string to picture
 * @Author: * 
 @CreateTime: * 
 @param IMGSTR Base64 encoded string
 * @ param path Picture paths-specific to file
 * @return
*
/public static Boolean Generateimage (string imgstr, String path) {
if    (imgstr = null)
return false;
Base64decoder decoder = new Base64decoder ();
try {
//decrypt
byte[] b = Decoder.decodebuffer (IMGSTR);
Process data for
(int i = 0; i < b.length ++i) {
if (B[i) < 0) {
b[i] + = 256;
}
OutputStream out = new FileOutputStream (path);
Out.write (b);
Out.flush ();
Out.close ();
return true;
catch (Exception e) {return
false;
}
}

Decryption naturally also has encryption. The following is encryption:

/**
 * @Description: Convert to Base64 encoded string based on picture address
 * @Author: * 
 @CreateTime: 
 * @return/
Public static string Getimagestr (String imgfile) {
    InputStream inputstream = null;
    byte[] data = null;
    try {
        InputStream = new FileInputStream (imgfile);
        data = new byte[inputstream.available ()];
        Inputstream.read (data);
        Inputstream.close ();
    } catch (IOException e) {
        e.printstacktrace ();
    }
    Encryption
    Base64encoder encoder = new Base64encoder ();
    return Encoder.encode (data);
}

Then put a test on the main function

/**
 * Example */public
static void Main (string[] args) {
    String strimg = getimagestr ("F:/86619-106.jpg" );
    System.out.println (strimg);
    Generateimage (strimg, "f:/86619-107.jpg");
}

It should be noted, however, that the Base64 encoded strings returned by the generic plugin have a prefix.

"Data:image/jpeg;base64," this has to be removed before decoding. (Note: The JPEG image format is different to do the corresponding transformation, may be jpg, PNG, etc.). There's a comma after Base64. )

Method Two, the front end uses the Multipart/form-data form, the back end uses the Springmvc commonsmultipartfile to receive.

Specific method Reference: http://blog.csdn.net/freyaalisa/article/details/75575130

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.