日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

【Spring MVC】文件上传、文件下载

發(fā)布時間:2024/2/28 javascript 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Spring MVC】文件上传、文件下载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

頁面效果:

一、文件下載

1.訪問資源時相應(yīng)頭如果沒有設(shè)置 Content-Disposition,瀏覽器默認按照 inline 值進行處理
1.1 inline 能顯示就顯示,不能顯示就下載.

2.只需要修改響應(yīng)頭中 Context-Disposition=”attachment;filename=文件名”
2.1 attachment 下載,以附件形式下載.
2.2 filename=值就是下載時顯示的下載文件名

3.實現(xiàn)步驟
3.1 導(dǎo)入apache 的兩個jar

3.2 在 jsp 中添加超鏈接,設(shè)置要下載文件

<a href="download?fileName=a.rar">下載</a>

3.2.1 在 springmvc.xml 中放行靜態(tài)資源 files 文件夾

<!-- 靜態(tài)資源 --><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 {// 設(shè)置響應(yīng)流中文件進行下載// attachment是以附件的形式下載,inline是瀏覽器打開// bbb.txt是下載時顯示的文件名res.setHeader("Content-Disposition", "attachment;filename=bbb.txt"); // 下載 // res.setHeader("Content-Disposition", "inline;filename=bbb.txt"); // 瀏覽器打開// 把二進制流放入到響應(yīng)體中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 把客戶端上傳的文件流轉(zhuǎn)換成MutipartFile 封裝類.
2.2 通過MutipartFile 封裝類獲取到文件流

3. 表單數(shù)據(jù)類型分類
3.1 在的enctype 屬性控制表單類型
3.2 默認值 application/x-www-form-urlencoded,普通表單數(shù)據(jù).(少量文字信息)
3.3 text/plain 大文字量時使用的類型.郵件,論文
3.4 multipart/form-data 表單中包含二進制文件內(nèi)容.(重要)

4. 實現(xiàn)步驟:
4.1 導(dǎo)入springmvc 包和apache 文件上傳 commons-fileupload 和 commons-io 兩個jar
4.2 編寫JSP 頁面

<h3>文件上傳</h3> <form action="upload" enctype="multipart/form-data" method="post">姓名:<input type="text" name="name"/><br/>文件:<input type="file" name="file"/><br/><input type="submit" value="提交"/> </form>

4.3 配置 springmvc.xml

<!-- MultipartResovler解析器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 設(shè)置文件最大字節(jié)數(shù) --><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 屬性值相同

@Controller public class DemoUpload {@RequestMapping("upload")public String upload(MultipartFile file,String name) throws IOException{String fileName = file.getOriginalFilename();String suffix = fileName.substring(fileName.lastIndexOf("."));// 限制上傳文件類型if(suffix.equalsIgnoreCase(".png")||suffix.equalsIgnoreCase(".txt")){String uuid = UUID.randomUUID().toString();FileUtils.copyInputStreamToFile(file.getInputStream(), new File("C:/picture/"+uuid+suffix));return "index.jsp";}else{return "error.jsp";}} }

附: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><!-- 注解驅(qū)動,相當(dāng)于配置了以下兩個類 --><!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping --><!-- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter --><mvc:annotation-driven></mvc:annotation-driven><!-- 放行靜態(tài)資源 --><!-- 訪問示例: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"><!-- 設(shè)置文件最大字節(jié)數(shù) --><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><!-- 自定義視圖解析器 --><!-- 當(dāng)跳轉(zhuǎn)語句添加前綴(比如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分發(fā)器 --><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 初始化參數(shù)名 --><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><!-- 字符編碼過濾器 --><!-- 配置文件無關(guān)順序問題,加載時就被實例化,等待tomcat回調(diào) --><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>

附:文件目錄結(jié)構(gòu)

總結(jié)

以上是生活随笔為你收集整理的【Spring MVC】文件上传、文件下载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。