Key points:
1, using the POST request
2,consumes=mediatype.multipart_from_data_value
3, the string inside the @RequestParm is consistent with the name value of the front input control
Examples of uploading files:
@PostMapping (value= "/photos", consumes=mediatype.multipart_from_data_value)publicVoid throws exception{ // Save file new fileoutputstream ("target/ ", Imgfile.getoriginalfilename ()); Ioutils.copy (Imgfile.getinputstream (), FOS); Fos.close (); }
Sample upload file with return value (return binary data to let users see what the uploaded image looks like):
@PostMapping (value= "/icon", consumes=mediatype.multipart_from_data_value) PublicByte[] AddIcon (@RequestParm ("photo") Multipartfile Imgfile)throwsexception{//binary data for user-uploaded filesInputStream is=Imgfile.getinputstream (); //Save FileFileOutputStream Fos=NewFileOutputStream ("target/", Imgfile.getoriginalfilename ()); Ioutils.copy (is, FOS); Fos.close (); //return file returnIoutils.tobytearray (IS); }
Sample Download File:
@GetMapping (value= "/{userid}/photos", consumes=mediatype.multipart_from_data_value) Public voidthrows exception{ // return file String userphotoimgsrc = "src/test/ 10001.jpg "; New FileInputStream (USERPHOTOIMGSRC); return Ioutils.tobytearray (IS);}
Springboot File Upload Download