Springmvc Upload Quiz

Source: Internet
Author: User

    1. Create a project

    2. Importing JAR Packages

    3. Configure Web. xml


1.web.xml Configuration


<?xml version= "1.0"  encoding= "UTF-8"?><web-app  Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"  xmlns= "Http://java.sun.com/xml/ns/javaee"   xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemalocation= "Http://java.sun.com/xml /ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd " id=" webapp_id " version=" 2.5 ">   <servlet><servlet-name>springmvc</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> contextconfiglocation</param-name><param-value>classpath:springmvc.xml</param-value></ init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping>< Servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping ></web-app> 

2.springmvc.xml Configuration, note that if you customize the location, you need to place it under Web-inf, the name must be Servlet-name name-servlet.xml

<?xml version= "1.0"  encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/ context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/ Schema/mvc/spring-mvc-3.1.xsd ><!--  Note driver  --><mvc:annotation-driven/><!--  annotation Scanning  --><context:component-scan base-package= "Com.springmvc"/><!--  View Controller  --> <bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" ><property  Name= "prefix"  value= "/view/" ></property><proPerty name= "suffix"  value= ". jsp" ></property></bean><!--  file last configuration    The ID name must be  multipartresolver--><bean id= "Multipartresolver"  class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "><!--  default upload size 2m -->< Property name= "Maxuploadsize"  value= "2097152" ></property></bean></beans>

3.index.jsp page

<%@ page language= "java"  contenttype= "Text/html; charset=utf-8"      pageencoding= "UTF-8"%> <%string path = request.getcontextpath (); String basepath = request.getscheme ()  +  "://" + request.getservername ()  +   ":"  + request.getserverport () + path +  "/";%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">

4. Springmvcupload Action

Package Com.springmvc;import Java.io.file;import Java.io.ioexception;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import org.springframework.web.multipart.multipartfile;@ Controller@requestmapping (value= "/upload/") public class Springmvcupload {@RequestMapping (value= ' up.do ') public String inputupload (@RequestParam (value= "InputName", Required=true) Multipartfile InputName) {try { Inputname.transferto (New File ("f://" +inputname.getoriginalfilename ())); catch (IllegalStateException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return "redirect:/view/success.jsp";}}

5.success.jsp page, placed under the View folder

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


Attention:

1. Upload must be post

Attribute enctype= "Multipart/form-data" must be in 2.form

3. The method of the last action must be

@RequestParam (value= "InputName", Required=true) multipartfile Mf

4. Configuring in Spirngmv.xml

<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsM Ultipartresolver ">

<!--default upload size 2M--

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

</bean>

The ID name here must be Multipartresolver

The upload size defaults to 2M, in bytes.

This article is from the "11134439" blog, please be sure to keep this source http://11144439.blog.51cto.com/11134439/1927821

Springmvc Upload Quiz

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.