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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - 在SpringMVC框架下實現文件的上傳和下載示例

在SpringMVC框架下實現文件的上傳和下載示例

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

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

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

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 請求轉為 DELETE、PUT 請求 -->
   <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">
  
  <!-- 配置自動掃描的包 -->
  <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 上下文中定義一個 DefaultServletHttpRequestHandler,
    它會對進入 DispatcherServlet 的請求進行篩查, 如果發(fā)現是沒有經過映射的請求, 就將該請求交由 WEB 應用服務器默認的
    Servlet 處理. 如果不是靜態(tài)資源的請求,才由 DispatcherServlet 繼續(xù)處理
 
    一般 WEB 應用服務器默認的 Servlet 的名稱都是 default.
    若所使用的 WEB 服務器的默認 Servlet 名稱不是 default,則需要通過 default-servlet-name 屬性顯式指定
    
  -->
  <mvc:default-servlet-handler/>
  
  <!-- 一般都會配置這個 <mvc:annotation-driven ></mvc:annotation-driven>,
  由于。。。requestmapping請求實現不了,使用這個,會使requestmapping請求一定實現
  -->
  <mvc:annotation-driven ></mvc:annotation-driven>
  <!-- 配置 MultipartResolver ,即配置文件上傳的屬性-->
   <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 默認的字符編碼 -->
    <property name="defaultEncoding" value="UTF-8"></property>
    <!-- 上傳文件的大小 ,最大上傳大小-->
    <property name="maxUploadSize" value="1024000"></property>
   </bean>
 </beans>

 handler類方法:實現文件的上傳和下載的方法

?
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;
  //實現文件的下載
  //需要說明的是文件的上傳和下載不需要其他配置
  @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();
    //響應頭的名字和響應頭的值
    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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男人狂擦女人的下面视频 | re99热| 欧美影院一区二区三区 | 999久久久| 国产在线精品成人一区二区三区 | 奇米9999 | 日韩 视频在线播放 | 精品国产欧美一区二区五十路 | 西野翔全部作品在线观看 | 丰满大屁股美女一级毛片 | 欧美黑人换爱交换乱理伦片 | 国产精品久久久久久福利 | 欧美高清乌克兰精品另类 | 久久亚洲高清观看 | 国产成人精品免费午夜 | 69罗莉视频在线观看 | 喜爱夜蒲2三级做爰 | 99久久99久久久精品齐齐鬼色 | 亚洲va精品中文字幕 | 草草在线视频 | 日本三级免费网站 | 草莓视频旧版本 | 国产99视频精品免费视频7 | 99在线在线视频免费视频观看 | 五月天久久久 | 398av影院视频在线 | 精品四虎 | 俄罗斯美女毛茸茸bbwbbw | 男人天堂999 | 天天操天天做 | 亚洲欧美日韩中文字幕网址 | 亚洲v日韩v欧美在线观看 | 91精品国产91热久久久久福利 | 久久中文电影 | 日韩一区二区三区四区五区 | 亚洲大片在线观看 | 91看片在线观看 | 亚洲乱码一二三四五六区 | 国产日韩精品一区二区三区 | 91精品国产色综合久久 | 门房秦大爷在线阅读 |