javascript
【Spring MVC】文件上传、文件下载
頁面效果:
一、文件下載
1.訪問資源時相應頭如果沒有設置 Content-Disposition,瀏覽器默認按照 inline 值進行處理
1.1 inline 能顯示就顯示,不能顯示就下載.
2.只需要修改響應頭中 Context-Disposition=”attachment;filename=文件名”
2.1 attachment 下載,以附件形式下載.
2.2 filename=值就是下載時顯示的下載文件名
3.實現步驟
3.1 導入apache 的兩個jar
3.2 在 jsp 中添加超鏈接,設置要下載文件
3.2.1 在 springmvc.xml 中放行靜態資源 files 文件夾
<!-- 靜態資源 --><mvc:resources location="/js/" mapping="/js/**"></mvc:resources><mvc:resources location="/css/" mapping="/css/**"></mvc:resources><mvc:resources location="/images/" mapping="/images/**"></mvc:resources><mvc:resources location="/files/" mapping="/files/**"></mvc:resources>3.3 編寫控制器方法
Java示例
@Controller public class DemoDownload {@RequestMapping("download")public void download(String filename, HttpServletResponse res, HttpServletRequest req) throws IOException {// 設置響應流中文件進行下載// attachment是以附件的形式下載,inline是瀏覽器打開// bbb.txt是下載時顯示的文件名res.setHeader("Content-Disposition", "attachment;filename=bbb.txt"); // 下載 // res.setHeader("Content-Disposition", "inline;filename=bbb.txt"); // 瀏覽器打開// 把二進制流放入到響應體中ServletOutputStream os = res.getOutputStream();System.out.println("here download");String path = req.getServletContext().getRealPath("files");System.out.println("path is: " + path);System.out.println("fileName is: " + filename);File file = new File(path, filename);byte[] bytes = FileUtils.readFileToByteArray(file);os.write(bytes);os.flush();os.close();} }JSP示例
<a href="download?filename=a.txt">點擊下載</a><br/>二、文件上傳
1. 基于apache 的commons-fileupload.jar 完成文件上傳.
2. MultipartResovler 作用:
2.1 把客戶端上傳的文件流轉換成MutipartFile 封裝類.
2.2 通過MutipartFile 封裝類獲取到文件流
3. 表單數據類型分類
3.1 在的enctype 屬性控制表單類型
3.2 默認值 application/x-www-form-urlencoded,普通表單數據.(少量文字信息)
3.3 text/plain 大文字量時使用的類型.郵件,論文
3.4 multipart/form-data 表單中包含二進制文件內容.(重要)
4. 實現步驟:
4.1 導入springmvc 包和apache 文件上傳 commons-fileupload 和 commons-io 兩個jar
4.2 編寫JSP 頁面
4.3 配置 springmvc.xml
<!-- MultipartResovler解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 設置文件最大字節數 --><property name="maxUploadSize" value="1024"></property></bean><!-- 異常解析器 --><bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="exceptionMappings"><props><prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error.jsp</prop></props></property></bean>4.4 編寫控制器類
4.4.1 MultipartFile 對象名必須和的name 屬性值相同
附: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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="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.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 掃描注解 --><context:component-scanbase-package="cn.hanquan.controller"></context:component-scan><!-- 注解驅動,相當于配置了以下兩個類 --><!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping --><!-- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter --><mvc:annotation-driven></mvc:annotation-driven><!-- 放行靜態資源 --><!-- 訪問示例:http://localhost:8080/springmvc_test/test/jquery.js --><mvc:resources location="/js/" mapping="/test/**"></mvc:resources><mvc:resources location="/js/" mapping="/js/**"></mvc:resources><mvc:resources location="/css/" mapping="/css/**"></mvc:resources><mvc:resources location="/images/" mapping="/images/**"></mvc:resources><mvc:resources location="/images/" mapping="/files/**"></mvc:resources><!-- MultipartResovler解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 設置文件最大字節數 --><property name="maxUploadSize" value="1024"></property></bean><!-- 異常解析器 --><bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="exceptionMappings"><props><prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error.jsp</prop></props></property></bean><!-- 自定義視圖解析器 --><!-- 當跳轉語句添加前綴(比如forward:)時,自定義視圖解析器無效,走默認視圖解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 添加前綴后,寫相對路徑不用加/ --><property name="prefix" value="/"></property><!-- 后綴沒配,與不寫相同,可以配成jsp --><property name="suffix" value=""></property></bean> </beans>附:web.xml 完整配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><!-- 配置前端控制器 --><servlet><servlet-name>abc</servlet-name><!-- servlet分發器 --><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>abc</servlet-name><!-- '/'表示除了jsp都攔截 --><url-pattern>/</url-pattern></servlet-mapping><!-- 字符編碼過濾器 --><!-- 配置文件無關順序問題,加載時就被實例化,等待tomcat回調 --><filter><filter-name>encoding</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app>附:文件目錄結構
總結
以上是生活随笔為你收集整理的【Spring MVC】文件上传、文件下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Spring】SpringMVC 作用
- 下一篇: 【Spring MVC】自定义拦截器 I