Springboot File Upload Download

Source: Internet
Author: User

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

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.