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