在SpringMVC框架下實(shí)現(xiàn)文件的上傳和下載示例
在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> <h3>Success page</h3> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringMVC+Ajax實(shí)現(xiàn)文件批量上傳和下載功能實(shí)例代碼
- MyBatis與SpringMVC相結(jié)合實(shí)現(xiàn)文件上傳、下載功能
- SpringMVC實(shí)現(xiàn)文件的上傳和下載實(shí)例代碼
- springMVC配置環(huán)境實(shí)現(xiàn)文件上傳和下載
- SpringMVC下實(shí)現(xiàn)Excel文件上傳下載
- SpringMVC框架實(shí)現(xiàn)圖片上傳與下載
- SpringMVC實(shí)現(xiàn)文件上傳和下載功能
- SpringMVC實(shí)現(xiàn)文件上傳和下載的工具類
- SpringMvc3+extjs4實(shí)現(xiàn)上傳與下載功能
- SpringMVC實(shí)現(xiàn)上傳下載文件
相關(guān)文章
Java動(dòng)態(tài)規(guī)劃方式解決不同的二叉搜索樹
二叉搜索樹作為一個(gè)經(jīng)典的數(shù)據(jù)結(jié)構(gòu),具有鏈表的快速插入與刪除的特點(diǎn),同時(shí)查詢效率也很優(yōu)秀,所以應(yīng)用十分廣泛。本文將詳細(xì)講講二叉搜索樹的原理與實(shí)現(xiàn),需要的可以參考一下2022-10-10
JAVA使用動(dòng)態(tài)代理對(duì)象進(jìn)行敏感字過濾代碼實(shí)例
這篇文章主要介紹了JAVA使用動(dòng)態(tài)代理對(duì)象進(jìn)行敏感字過濾代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
java JVM原理與常識(shí)知識(shí)點(diǎn)
在本文中小編給大家分享的是關(guān)于java的JVM原理和java常識(shí),有興趣的朋友們可以學(xué)習(xí)下2018-12-12
Spring?使用注解存儲(chǔ)和讀取?Bean對(duì)象操作方法
在?Spring?中,要想更加簡(jiǎn)單的實(shí)現(xiàn)對(duì)?Bean?對(duì)象的儲(chǔ)存和使用,其核心就是使用?注解?,本文主要就是演示如何使用注解實(shí)現(xiàn)對(duì)?Bean?對(duì)象的存取操作,感興趣的朋友跟隨小編一起看看吧2023-08-08
SpringBoot通過JSON傳遞請(qǐng)求參數(shù)的實(shí)例詳解
這篇文章主要介紹了SpringBoot通過JSON傳遞請(qǐng)求參數(shù),示例介紹SpringMVC如何通過JSON格式傳遞入?yún)?,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11

