Submit Multipart/form-data Type Data

Source: Internet
Author: User
Tags file upload getmessage object object mysql database
Submit Multipart/form-data Type data

@ (spring MVC) [File Upload | Additional information] example: ICBC Signature certificate upload save

Description: The framework uses Spring MVC 4, the Interface test tool uses postman, this article to implement the function is: file upload : File upload information filled in the Form-data area, the field name is the file name, The file content is selected by the postman control, stored in the MySQL database type blob, the corresponding Java class property is byte[] file storage : Directly store the file to the database, not the URL of the stored file; Other information : Additional information for uploading files is also saved to the database. Postman Test Request

Postman Interface Address:
https://www.getpostman.com/collections/3ff9e71924f3facec051

Controller Source Code

@RequestMapping (value = "Business/{businessid}/{company}/mode", method = Requestmethod.post, produces = Mediat Ype.
                                 Application_json_value) public responseentity Create (@PathVariable ("Businessid") Long Businessid,
                                 @PathVariable ("Company") string company, @RequestParam ("name") string name, @RequestParam ("password") String Privatepassword, @Reque
            Stparam ("account") String account, HttpServletRequest request) {try {
            Boolean passed = Companyutil.checkcompany (company);
                if (passed) {paymodedto modedto = new Paymodedto ();
                Modedto.setname (name);
                Modedto.setbusinessid (BUSINESSID);
                Modedto.setprivatepassword (Privatepassword);

                Modedto.setaccount (account); Diskfileitemfactory factory = new DiskFileitemfactory ();
                Servletfileupload upload = new Servletfileupload (factory);
                List items = upload.parserequest (request);
                    For (object object:items) {Fileitem Fileitem = (fileitem) Object;
                    System.out.println (Fileitem.tostring ());
                    if (Fileitem.getfieldname (). Equals ("PublicKey")) {Modedto.setpublickey (Fileitem.get ()); } else if (Fileitem.getfieldname (). Equals ("Privatekey")) {Modedto.setprivatekey (Filei
                    Tem.get ());
                }} logger.info ("Client will create a payment configuration: {}", jsonobject.tojsonstring (modedto));
                    if (Company.equals (ICONSTANT.ICBC)) {Logger.info ("will be added to ICBC payment Configuration");
                    Paymodeservice.createicbcpaymode (modedto);
                Logger.info ("Successfully added ICBC payment Configuration");
        } else if (Company.equals (Iconstant.mobao)) {            Logger.info ("will be added is the Magic Treasure Payment Configuration");
                    Paymodeservice.createmobaopaymode (modedto);
                Logger.info ("Successfully added Magic Treasure Payment Configuration");
            } return new Responseentity (new Bingo (), httpstatus.created);
                } else {logger.info ("Check the payment company for short path variable {} does not pass", companies);
                Jsonobject error = new Jsonobject ();
                Error.put ("ErrorCode", 400);
                Error.put ("ErrorMsg", "Company short fill Error");
            return new Responseentity (Error, httpstatus.bad_request);
            }} catch (Fileuploadexception e) {e.printstacktrace ();
            Logger.info (E.getmessage ());
            Jsonobject error = new Jsonobject ();
            Error.put ("ErrorCode", 400);
            Error.put ("ErrorMsg", E.getmessage ());
        return new Responseentity (Error, httpstatus.bad_request);
            } catch (Coreexception e) {e.printstacktrace (); Logger.info (Jsonobject.tojsonString (e));
        return new Responseentity (e, httpstatus.bad_request);
            } catch (Exception e) {e.printstacktrace ();
            Logger.info (E.getmessage ());
            Jsonobject error = new Jsonobject ();
            Error.put ("ErrorCode", 400);
            Error.put ("ErrorMsg", E.getmessage ());
        return new Responseentity (Error, httpstatus.bad_request); }
    }
My Contact informationQ q:1250052380 e-mail: 1250052380@qq.com

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.