In the Java EE environment in eclipse: import the necessary rack packages
Config file for Web. xml:
<?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"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" > <!--configuration Springmvc dispatcherservlet--<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-o n-startup> </servlet> <servlet-mapping> <servlet-name>springdispatcherservlet</servle T-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--configuration Hiddenhttpmethodfil ter: Convert POST request to DELETE, put request-<filter> <filter-name>hiddenhttpmethodfilter</filter-na Me> <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>
Spring's bean configuration file springmvc.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><!--Configure automatic scanning of packages--<context:component-scan base- Package= "Com.atguigu.springmvc" ></context:component-scan> <!--configuration View Resolver--<beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" value= "/ web-inf/views/"></property> <property name=" suffix "value=". jsp "></property> </bean> <!--default-servlet-handler will define a Defaultservlethttprequesthandler in the SPRINGMVC context, which will screen for requests to enter Dispatcherservlet if it finds no mapped requests , the request is referred to the WEB application server's default Servlet processing. If the request is not a static resource, the Dispatcherservlet continues to process the generic WEB application server The default Servlet name isdefault. If the default Servlet name for the WEB server you are using is notdefault, you need to passdefault-servlet-The Name property explicitly specifies--<MVC:default-servlet-handler/> <!--will typically configure this <mvc:annotation-driven ></mvc:annotation-driven>, as a result of ... Requestmapping request can not be implemented, using this, will make the requestmapping request must be implemented-<mvc:annotation-driven ></mvc:annotation-driven> </beans>
Handler class method: How to upload and download files
@Controller Public classspringmvctest {@AutowiredPrivateEmployeeDAO EmployeeDAO; //Implementing a file Download//It is necessary to note that the file upload and download do not require additional configuration@RequestMapping ("Testresponseentity") Publicresponseentity<byte[]> Testresponseentity (HttpSession session)throwsioexception{byte[] body=NULL; ServletContext ServletContext=Session.getservletcontext (); ///files/abc.txt: The address of the file you want to downloadInputStream In=servletcontext.getresourceasstream ("/files/abc.txt"); Body=New byte[In.available ()]; In.read (body); Httpheaders Headers=Newhttpheaders (); //The name of the response header and the value of the response headerHeaders.add ("Content-disposition", "Attachment;filename=abc.txt"); Httpstatus StatusCode=Httpstatus.ok; Responseentity<byte[]> response=Newresponseentity<byte[]>(body, headers, statusCode); returnresponse; } //implementing file Uploads@ResponseBody @RequestMapping ("/testhttpmessageconverter") Publicstring Testhttpmessageconverter (@RequestBody string body) {System.out.println (body); return"Helloworld!" +NewDate (); } }
JSP page: index.jsp:
<%@ page language= "java" contenttype= "text/html; charset=utf-8" pageencoding< /span>= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > file: <input type= "file" name= "file"/> Desc: <input t Ype= "text" name= "desc"/> <input type= "Submit" value= "Submit"/> </form> <br><br> < download!--files-<a href= "testresponseentity" >test responseentity</a> </center> &L T;/body>
Upload and download files in the SPRINGMVC framework