Java HTTP POST upload file

Source: Internet
Author: User

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

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.