Springmvc_ File Upload Example

Source: Internet
Author: User

began to contact SPRINGMVC, a little anticipation, a little excited. Single little thing about today almost made me fall apart. In this record, not to record what technical points, is purely to warn themselves, and then careful not too!

A small example was made:

Set up the Springmvc shelf, guide the relevant package (when it is necessary to make a file upload Commons-fileupload.jar this must be).

① Configure the Web. xml file:

<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"ID= "webapp_id"version= "3.1">        <!--Configuring the Springmvc dispatcherservlet -    <!--the front controller of this Spring WEB application, responsible-handling all application requests -    <servlet>        <Servlet-name>Springdispatcherservlet</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>    <!--Map All requests to the Dispatcherservlet for handling -    <servlet-mapping>        <Servlet-name>Springdispatcherservlet</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping>        <!--configuration hiddenhttpmethodfilter: Convert post to delete, put request -    <Filter>        <Filter-name>Hiddenhttpmethodfilter</Filter-name>        <Filter-class>Org.springframework.web.filter.HiddenHttpMethodFilter</Filter-class>    </Filter>         <filter-mapping>         <Filter-name>Hiddenhttpmethodfilter</Filter-name>         <Url-pattern>/*</Url-pattern>     </filter-mapping></Web-app>

② Configure the Springmvc.xml file:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.2.xsd ">    <!--Configuring an automatic Scan package -    <Context:component-scanBase-package= "Com.springmvc"></Context:component-scan>    <!--configuration is Diagram parser -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/web-inf/views/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean>        <!--configuration multipartresover: For file uploads -    <BeanID= "Multipartresover"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver">        < Propertyname= "Defaultencoding"value= "UTF-8"></ Property>        < Propertyname= "Maxuploadsize"value= "1024000"></ Property>    </Bean></Beans>

③ then start writing JSP files index.jsp:

<%@ 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 ">
<HTML> <Head>
<Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">
<title>Insert Title here</title> </Head>
<Body> <formID= "form"Action= "Testfileupload"Method= "Post"enctype= "Multipart/form-data">
File:<inputtype= "File"name= "File" />
Desc:<inputtype= "text"name= "desc" />
<inputtype= "Submit"value= "Submit" />
</form>
</Body>
</HTML>

④ Start writing Java code:

Package Com.springmvc.test;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;@ Controllerpublic class Springmvctest {@RequestMapping ("/testfileupload") public String testfileupload (@RequestParam ( Value= "desc", Required=false) String desc, @RequestParam (value= "file", Required=false) Multipartfile file) throws IOEXCEPTION{SYSTEM.OUT.PRINTLN ("desc:" + desc); System.out.println ("OriginalFilename:" + file.getoriginalfilename ()); System.out.println ("InputStream:" +file.getinputstream ()); return "Success";}}

The relevant files are well, there is a success.jsp to write, just write a page on the line.

Start running, a run on the wrong nullpoint, check the cause "System.out.println (" OriginalFilename: "+ file.getoriginalfilename ());" Null pointer, that is, file is null, how can it?

Related configuration and packages have Ah, step-by-step look down to find the configuration Springmvc.xml file ID is wrong, a "L", the ID should be "configuration Multipartresolver", Tragedy ah ... So write something must be careful ah, can copy the copy it as far as possible, otherwise a little notice on the sad urge ...

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.