Springmvc File Upload

Source: Internet
Author: User

@RequestMapping ("/upload")
Public String upload (HttpServletRequest request) throws IllegalStateException, ioexception{
HTTP-based file uploads: parsing the browser's request protocol
1. Create a parser
Commonsmultipartresolver multipartresolver = new Commonsmultipartresolver ();
2. Determine whether a file is included in the request to see if it belongs to a file form
Boolean flag = Multipartresolver.ismultipart (request);
if (flag) {
3.request Object Strong turn
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
4. Get files from the objects that have been transferred above
iterator<string> fileNames = Multipartrequest.getfilenames ();
5.while Loop Get every file
while (Filenames.hasnext ()) {
6. Get the file according to the filename you get
Multipartfile multipartfile = Multipartrequest.getfile ((String) Filenames.next ());
7. Determine if the file name is empty
String fileName = Multipartfile.getoriginalfilename ();
if (fileName! = null) {
8. Construct the upload path
String Path = "e:/upload/" + fileName;
File File = new file (path);
9. Transferring files
Multipartfile.transferto (file);
}
}
}
Request.setattribute ("msg", "Upload success");
return "Forward:/file/jump";
}

@RequestMapping ("/jump")
Public String Jump (httpservletrequest Request,model Model) {
System.out.println ("jump page");
Model.addattribute ("msg", "Upload success");
return "/success";
}

Springmvc File Upload

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.