Springmvc multipartfile file upload and parameter acceptance __java

Source: Internet
Author: User

Springmvc file upload, first two basis, 1. Form form Properties plus enctype= "Multipart/form-data"

Emphasis: Form form's <form method= "post" ..., method must have, I am here to use is post, as for get row not tried, no method= "POST" also will report not multipart request error.


2. Configure Multipartresolver in configuration file


A file exceeding the limit throws an exception before entering the controller, which does not affect the configuration within the allowable range



3. A simple way of receiving, thinking: Multipartfile accept the file and through the IO binary stream (Multipartfile.getinputstream ()) input to Fileoutstream Save the file, then do what you do

The parameter receives the same as the Multipartfile receive.

Accept the files and parameters named file and ID in the form forms screenshot. As follows

@RequestMapping (value = "Attendee_uploadexcel.do")

@ResponseBody

public void Uploadexcel (@RequestParam ("file") Multipartfile file, @RequestParam ("id") String ID) throws Exception {

Form form Submit parameter test is string type

if (file = = null) return;

String fileName = File.getoriginalfilename ();

String path = Getrequest (). Getservletcontext (). Getrealpath ("/upload/excel");

Gets the actual path of the specified file or folder in the project, Getrequest () This method is to return a httpservletrequest, encapsulating this method in order to deal with the coding problem

FileOutputStream fos = fileutils.openoutputstream (new File (path+ "/" +filename));//Open Fileoutstrean stream

Ioutils.copy (File.getinputstream (), FOS);//convert multipartfile file into a binary stream and enter the Fileoutstrean

Fos.close ()//

......

}


Other methods, will httpservletrequest req strong into Multiparthttpservletrequest req after req.getparameter ("id");

HttpServletRequest request;

Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;

Multipartfile file = multipartrequest.getfile ("file");

String id = multipartrequest.getparameter ("id");

String fileName = File.getoriginalfilename (); .........





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.