一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 在SpringMVC框架下實(shí)現(xiàn)文件的上傳和下載示例

在SpringMVC框架下實(shí)現(xiàn)文件的上傳和下載示例

2020-08-13 12:08G-&-D Java教程

本篇文章主要介紹了在SpringMVC框架下實(shí)現(xiàn)文件的上傳和下載示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

在eclipse中的javaEE環(huán)境下:導(dǎo)入必要的架包

web.xml的配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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)文件的上傳和下載的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ 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頁面:顯示文件上傳成功

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ 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>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/lxnlxn/p/5938861.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 好看华人华人经典play | 白丝校花掀起短裙呻吟小说 | 日本888xxxx| 国模李丽莎大尺度啪啪 | 人人爽人人看 | 日本午夜大片免费观看视频 | 亚洲国产精品嫩草影院永久 | 暖暖日本在线观看免费 | 青草青草伊人精品视频 | 国产高清免费午夜在线视频 | 欧美精品一国产成人性影视 | 日本不卡高清免费v日本 | 男生同性视频twink在线 | 精品视频一区二区三区 | 国产欧美日韩一区二区三区在线 | 2022最新国产在线 | 国产一卡2卡3卡四卡高清 | 久久观看视频 | 国产成人久久久精品一区二区三区 | 97精品国产高清在线看入口 | 97青草 | 日麻逼 | 网红思瑞一区二区三区 | 国产精品美女福利视频免费专区 | 8天堂资源在线官网 | 亚洲青草视频 | 亚洲精品国产精品国自产观看 | 国产精品一二三 | 日日操天天爽 | 帅老头恋帅老头同性tv | 日本护士厕所xxx | 毛片影院 | 午夜伦伦电影理论片费看 | 色视频亚洲| 91久久福利国产成人精品 | 狠狠搞视频 | xxxx意大利xxxxhd| 男人操美女逼视频 | 亚洲视频在线一区二区三区 | 亚洲成人免费观看 | 欧美贵妇videos办公室360 |