您好,登錄后才能下訂單哦!
在eclipse中的javaEE環(huán)境下:導(dǎo)入必要的架包
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"> <!-- 配置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-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置 HiddenHttpMethodFilter: 把 POST 請(qǐng)求轉(zhuǎn)為 DELETE、PUT 請(qǐng)求 --> <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>
spring的bean的配置文件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/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.0.xsd"> <!-- 配置自動(dòng)掃描的包 --> <context:component-scan base-package="com.atguigu.springmvc"></context:component-scan> <!-- 配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- default-servlet-handler 將在 SpringMVC 上下文中定義一個(gè) DefaultServletHttpRequestHandler, 它會(huì)對(duì)進(jìn)入 DispatcherServlet 的請(qǐng)求進(jìn)行篩查, 如果發(fā)現(xiàn)是沒有經(jīng)過映射的請(qǐng)求, 就將該請(qǐng)求交由 WEB 應(yīng)用服務(wù)器默認(rèn)的 Servlet 處理. 如果不是靜態(tài)資源的請(qǐng)求,才由 DispatcherServlet 繼續(xù)處理 一般 WEB 應(yīng)用服務(wù)器默認(rèn)的 Servlet 的名稱都是 default. 若所使用的 WEB 服務(wù)器的默認(rèn) Servlet 名稱不是 default,則需要通過 default-servlet-name 屬性顯式指定 --> <mvc:default-servlet-handler/> <!-- 一般都會(huì)配置這個(gè) <mvc:annotation-driven ></mvc:annotation-driven>, 由于。。。requestmapping請(qǐng)求實(shí)現(xiàn)不了,使用這個(gè),會(huì)使requestmapping請(qǐng)求一定實(shí)現(xiàn) --> <mvc:annotation-driven ></mvc:annotation-driven> <!-- 配置 MultipartResolver ,即配置文件上傳的屬性--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默認(rèn)的字符編碼 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 上傳文件的大小 ,最大上傳大小--> <property name="maxUploadSize" value="1024000"></property> </bean> </beans>
handler類方法:實(shí)現(xiàn)文件的上傳和下載的方法
@Controller public class SpringMVCTest { @Autowired private EmployeeDao employeeDao; //實(shí)現(xiàn)文件的下載 //需要說明的是文件的上傳和下載不需要其他配置 @RequestMapping("testResponseEntity") public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException{ byte[] body=null; ServletContext servletContext=session.getServletContext(); ///files/abc.txt:所要下載文件的地址 InputStream in=servletContext.getResourceAsStream("/files/abc.txt"); body=new byte[in.available()]; in.read(body); HttpHeaders headers=new HttpHeaders(); //響應(yīng)頭的名字和響應(yīng)頭的值 headers.add("Content-Disposition", "attachment;filename=abc.txt"); HttpStatus statusCode=HttpStatus.OK; ResponseEntity<byte[]> response=new ResponseEntity<byte[]>(body, headers, statusCode); return response; } //文件上傳, @RequestMapping("/testFileUpload") public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException{ System.out.println("desc:"+desc); System.out.println("OriginalFilename"+file.getOriginalFilename()); System.out.println("InputStream"+file.getInputStream()); return "success"; } }
jsp頁面: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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <!-- 文件上傳的表單 --> <form action="testFileUpload" method="post" enctype="multipart/form-data"> File:<input type="file" name="file"/> Desc:<input type="text" name="desc"/> <input type="submit" value="Submit"/> </form> <br><br> <!-- 文件的下載 --> <a href="testResponseEntity" rel="external nofollow" >Test ResponseEntity</a> </center> </body> </html>
success.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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h4>Success page</h4> </body> </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。