Springmvc File Upload Example

Source: Internet
Author: User
Tags uuid

The first thing to do is to guide the package, using the package is:

Commons-fileupload-*.*.*.jar

Commons-io-*.*.jar

* Number represents the version number

Here to share with you download link: https://files.cnblogs.com/files/chuanyueinlife/SpringMVC%E4%B8%8A%E4%BC%A0jar%E5%8C%85.zip

JSP page:

<formAction= "Upload.do"Method= "POST"enctype= "Multipart/form-data">Select File:<inputtype= "File"name= "File"> 
<BR>
<inputtype= "Submit"value= "Submit"> </form>

Controller:

 PackageController;ImportJava.io.File;Importjava.io.IOException;ImportJava.util.UUID;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;Importorg.springframework.web.multipart.MultipartFile; @Controller Public classUploadcontroller {@RequestMapping ("Upload.do")     PublicString upload (HttpServletRequest request, String userName, @RequestParam (value = "File") multipartfile file)throwsIllegalStateException, IOException {String Upload_path= Request.getsession (). Getservletcontext (). Getrealpath ("Upload"); String file_name=File.getoriginalfilename (); file_name= File_name.substring (file_name.lastindexof ("\ \") + 1); String Final_pathname= Upload_path + "\" + uuid.randomuuid () + "-" +file_name; File F=NewFile (final_pathname);        File.transferto (f); return"Forward:index.jsp"; }}

Springmvc.xml File configuration:

<!--File Upload configuration -    <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver">        < Propertyname= "Maxuploadsize">            <value>10240000</value>        </ Property>        </Bean>

Springmvc File Upload Example

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.