1. Upload interface
@IgnoreToken @RequestMapping (value= "/upload/cpicfile", method =requestmethod.post) Public voidCpicfile (httpservletresponse response, HttpServletRequest request) {ErrorCode ErrorCode=errorcode.failed; Map<String,Object> Reqdata = This. getcontent (Request); String Path= Sysconf.cpic_workbasedir;//Upload Path Try{String name= (String) reqdata.get ("name"); String content= (String) reqdata.get ("content"); byte[] bytes =org.apache.commons.codec.binary.Base64.decodeBase64 (content); //Write FileFile filetmp =NewFile (path + name);//Temporary FilesFileOutputStream FOP =NewFileOutputStream (filetmp); if(!filetmp.exists ()) {Filetmp.createnewfile (); } fop.write (bytes); Fop.flush (); Fop.close (); System.err.println ("Done"); ErrorCode=errorcode.success; } Catch(Exception e) {logger.error ("Upload cpic Picture exception", E); ErrorCode=Errorcode.sys_error; } sendresponsecontent (response, Reqdata, ErrorCode); }
2. Test the interface
@Test Public voidTestcpicuploadfile () {Try{Map<String,Object> parammap=NewHashmap<>(); Parammap.put ("Name", "Cpic-160714.xml"); File F=NewFile ("C:/sftp/cpic-20160713-3.xml"); InputStream in=NewFileInputStream (f); byte[] B =New byte[(int) F.length ()];//create an array of appropriate file sizesIn.read (b);//reads the contents of the file into the b[] arrayIn.close (); String s=NewString (b); System.out.println ("s =" +s); System.out.println ("B1 =" + B);//memory AddressString SS =org.apache.commons.codec.binary.Base64.encodeBase64String (b); Base64 Encoded SYSTEM.OUT.PRINTLN ("SS =" +SS); System.out.println ("B2 =" + base64.decodebase64 (ss));//memory AddressParammap.put ("Content", SS); //@IgnoreToken//parammap.put ("token", "54ungzbaach65xypcceq48gd6jwlyros"); //token can be added with an annotation. System.out.println ("parammap=" +Parammap); Httpresult Httpresult= Httputil.post ("/cpic/upload/cpicfile", Parammap); System.out.println (Httpresult); } Catch(Exception e) {e.printstacktrace (); } }
Java HTTP POST upload file