is a small knowledge point, but because a small detail wasted most of the day to fix,
@ResponseBody @RequestMapping ("/uploadjdjdimage") public Modelmap Uploadjdjdimage (httpservletresponse response, Modelmap model, @RequestBody string jsonstring) { string code= " ", msg=" "; Boolean false ; Try { String json= Urldecoder.decode (jsonstring, "Utf-8"); Jsonobject jsonobjnew jsonobject (JSON); String Photo= jsonobj.getstring ("photo");
My side is to use the Java service to receive the android side of the photo, photo format for the binary image processing with base64, according to the general idea is to receive directly after the generation of a picture file,
Then write the data in a stream,
/** upload photo */public string upload (string photo, httpservletresponse response, String savepath) {Response.setcontenttype (" Text/html "); Bufferedoutputstream BOS = NULL; File newFile = null; String filename= "", try {file file= new file (Savepath), if (!file.exists ()) {///Determine if there is a path under this file File.mkdir ();//Create the directory specified by this abstract pathname. }if (Photo.length () >0) {byte [] by= null; Base64decoder base64= new Base64decoder (); by= base64.decodebuffer (photo); String newpath= savepath + "\ \" + system.currenttimemillis () + ". jpg"; newfile= new File (NewPath); bos= new Bufferedoutputstream (New FileOutputStream (NewFile)); Bos.write (by); Bos.flush (); Filename=newfile.getname ();}} catch (Exception e) {e.printstacktrace ();} FINALLY{IF (BOS! = null) {try {bos.close ();} catch (IOException E1) {e1.printstacktrace ();}}} return fileName;}
However, looked under the path below the picture written, incredibly black, the service also did not throw an exception, in the online search for other pictures, basically all the same written
Picture, and then very confused, and then asked the elder to understand that the received binary data will also need to handle the characters
String json= Urldecoder.decode (jsonstring, "utf-8"); Jsonobject jsonobj= New Jsonobject (JSON); String photo= jsonobj.getstring ("photo");p hoto= photo.replace ("Data:image/png;base64", "");p hoto= Photo.replaceall ( "Data:image/jpeg;base64", "");p hoto= photo.replace ("", "+");//Replace empty string for +photo= photo.replace ("\ n", ""); Empty line break photo= photo.replace ("\ T", "");p hoto= photo.replace ("\ R", ""),//photo= photo.replace ("/", ""); The worry escape character
Need to replace some special characters, this should be considered a relatively cold little knowledge point, the record here, hoping to help the students behind, the last secondary PHP binary image data
The processing
function Img ($str, $imgName =null)//base64 generate picture save { if ($imgName) { unlink (root_path. ') /public/uploads/'. $imgName); Delete Original } $saveName = Request ()->time (). rand_string (4). '. jpg '; The image name is generated $str = Str_replace (', ' + ', $str);//Replace empty string with + $str = str_replace (' \ n ', ' ', $str); Empty line break $str = Str_replace (' \ t ', ' ', $str); $str = Str_replace (' \ R ', ' ', $str); $str = Stripslashes ($STR); the Str_replace escape character $str = (' Data:image/jpeg;base64, ', ', $str); $str = Str_replace (' data:image/png;base64, ', ', $str); $img = Base64_decode ($STR); Decode file_put_contents (Root_path. '/public/uploads/'. $saveName, $img); Save Picture $img _url = '/uploads/'. $saveName; Generate picture Save path return $img _url; }
Java Uploading photo Attachments