Springmvc File Upload

Source: Internet
Author: User

1.1. Configure the virtual directory

Configure the picture virtual directory on Tomcat and add it under Tomcat under conf/server.xml :

<context docbase= "D:\develop\upload\temp" path= "/pic" reloadable= "false"/>

access http://localhost:8080/pic to access the pictures under D: \develop\upload\temp.

You can also configure through Eclipse , such as:


1.2. Configuring the Upload parser

Configuring the File Upload parser in springmvc.xml

<!--file upload, id must be set to Multipartresolver--

<bean id="Multipartresolver"


<!--set File upload Size--

<property name="maxuploadsize" value="5000000" />


Set up a form to upload files, such as:


@RequestMapping (value = "/updateitem.action")

Public Modelandview UpdateItem (items items) {This name is the same as the Name property of the JSP page

Public String UpdateItem (Queryvo vo,multipartfile picturefile) throws exception{

Save picture to

String name = Uuid.randomuuid (). toString (). ReplaceAll ("-", "");


String ext = filenameutils.getextension (Picturefile.getoriginalfilename ());

Picturefile.transferto (New File ("d:\\upload\\" + name + "." + ext));

Vo.getitems (). Setpic (name + "." + ext);


Itemservice.updateitemsbyid (Vo.getitems ());

Modelandview Mav = new Modelandview ();

Mav.setviewname ("Success");

Return "redirect:/itemedit.action?id=" + vo.getitems (). GetId ();

return "Forward:/item/itemlist.action";




<td> Product Images </td>


<c:if test= "${item.pic!=null}" >



<input type= "File" name= "PictureFile"/>



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.