Springmvc multipartfile file upload and parameter receive

Source: Internet
Author: User
Tags file upload


Reprinted from: http://blog.csdn.net/u013771277/article/details/47384817


Springmvc file Upload, first two base, 1. Add Enctype= "Multipart/form-data" to form form properties

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


2. Configuration file in configuration Multipartresolver


File out-of-bounds will throw an exception before entering the controller, without affecting the configuration within the allowed range



3. Simple Receive method, idea: Multipartfile accept the file and enter it into the fileoutstream by IO binary stream (Multipartfile.getinputstream ()) to save the file, then why do you do it?

The parameters receive the same as Multipartfile receive.

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

@RequestMapping (value = "Attendee_uploadexcel.do")

@ResponseBody

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

parameter of form form submission test to 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 that encapsulates this method in order to handle the encoding problem

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

Ioutils.copy (File.getinputstream (), FOS);//convert multipartfile file into binary stream and input to Fileoutstrean

Fos.close ();//

......

}


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

HttpServletRequest request;

Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;

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

String id = multipartrequest.getparameter ("id"); Receive parameters that are carried by the client incoming file

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.