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