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

服務(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-26 22:45MrSaber Java教程

這篇文章主要為大家詳細(xì)介紹了SpringMVC實(shí)現(xiàn)文件下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了SpringMVC文件下載的具體代碼,供大家參考,具體內(nèi)容如下

兩個案例

  1.為登錄用戶提供下載服務(wù)。

  2.阻止僅通過輸入網(wǎng)址即可獲取下載。

文件下載概覽

  為了將文件發(fā)送給瀏覽器,我們需要在控制器中完成以下操作:

  • 對請求處理方法使用void返回類型,并且在方法中添加HttpServletResponse參數(shù)。
  • 將響應(yīng)的內(nèi)容類型設(shè)為文件的內(nèi)容類型。Content-Type標(biāo)題在某個實(shí)體的body中定義數(shù)據(jù)的類型,并包含媒體類型和子類型標(biāo)識符。如果不清楚內(nèi)容類型,并且希望瀏覽器失始終顯示保存對話框,則將它設(shè)為APPLICATION/OCTET-STREAM。這個值時不區(qū)分大小寫的。
  • 添加一個名為Content-Disposition的HTTP響應(yīng)標(biāo)題,并賦值attachment;filename=fileName,這里的fileName是默認(rèn)的文件名。

案例1:為登錄用戶提供下載服務(wù)

Domain類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package domain;
public class Login {
 private String username;
 private String password;
 
 public String getUsername() {
  return username;
 }
 
 public void setUsername(String username) {
  this.username = username;
 }
 
 public String getPassword() {
  return password;
 }
 
 public void setPassword(String password) {
  this.password = password;
 }
}

Controller控制器

?
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package controller;
 
import domain.Login;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
 
@Controller
public class ResourceController {
 private static final Log logger = LogFactory.getLog(ResourceController.class);
 
 @RequestMapping(value = "/login")
 public String login(@ModelAttribute Login login, HttpSession session, Model model)
 {
  model.addAttribute("login",new Login());
  if("ms".equals(login.getUsername())&&"123".equals(login.getPassword()))
  {
   session.setAttribute("loggedIn",Boolean.TRUE);
   return "Main";
  }
  else
  {
   return "LoginForm";
  }
 }
 
 @RequestMapping(value = "/resource_download")
 public String downloadResource(HttpSession session, HttpServletRequest request, HttpServletResponse response)
 {
  if(session==null||session.getAttribute("loggedIn")==null)
  {
  return "LoginForm";
  }
  String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/data");
  File file = new File(dataDirectory,"Blog.zip");
  if(file.exists())
  {
   response.setContentType("application/octet-stream");
   response.addHeader("Content-Disposition","attachment;filename=Blog.zip");
   byte[] buffer = new byte[1024];
   FileInputStream fis =null;
   BufferedInputStream bis =null;
   try {
    fis = new FileInputStream(file);
    bis = new BufferedInputStream(fis);
    OutputStream os =response.getOutputStream();
    int i =bis.read(buffer);
    while (i!=-1) {
     os.write(buffer, 0, i);
     i=bis.read(buffer);
    }
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }finally {
    try {
     bis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
    try {
     fis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
 
  }
  return null;
 }
}

編寫視圖

Main.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
 <title>DownPage</title>
</head>
<body>
 <h4>點(diǎn)擊鏈接下載文件</h4>
 <p>
  <a href="resource_download" rel="external nofollow" >Down</a>
 </p>
</body>
</html>

LoginForm.jsp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
 <title>登錄頁面</title>
</head>
<body>
 <form:form commandName="login" action="login" method="post">
  賬號: <br>
  <form:input path="username" cssErrorClass="error" id="username"/>
  <br> 密碼: <br>
  <form:input path="password" cssErrorClass="error" id="password"/>
  <br> <input type="submit" value="提交">
 </form:form>
</body>
</html>

案例2:阻止僅通過輸入網(wǎng)址即可獲取下載

?
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
43
44
45
46
@RequestMapping(value = "/resource_download")
 public String downloadResource(HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestHeader String refuer
 ){
  if(refer==null) //通過判斷refer來瀏覽器輸入網(wǎng)址就能下載圖片的情況
  {
    return "LoginForm";
  }
  String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/data");
  File file = new File(dataDirectory,"Blog.zip");
  if(file.exists())
  {
   response.setContentType("application/octet-stream");
   response.addHeader("Content-Disposition","attachment;filename=Blog.zip");
   byte[] buffer = new byte[1024];
   FileInputStream fis =null;
   BufferedInputStream bis =null;
   try {
    fis = new FileInputStream(file);
    bis = new BufferedInputStream(fis);
    OutputStream os =response.getOutputStream();
    int i =bis.read(buffer);
    while (i!=-1) {
     os.write(buffer, 0, i);
     i=bis.read(buffer);
    }
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }finally {
    try {
     bis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
    try {
     fis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
 
  }
  return null;
 }
}

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美1区 | 全肉一女n男np高h乳 | 俄罗斯一级淫片bbbb | 狠狠撸在线影院 | 日韩精选视频 | 亚洲欧美日韩一区成人 | 好吊日在线 | 亚洲bt区| 美女舒服好紧太爽了视频 | 我的美女奴隶 | 亚洲精品久久玖玖玖玖 | 美女女女女女女bbbbbb毛片 | 91精品国产高清久久久久久 | 国语刺激对白勾搭视频在线观看 | 亚洲欧美国产精品久久久 | 欧美破苞合集 magnet | 五月精品 | 精品一区二区三区免费视频 | 色老板在线免费视频 | sedog在线长片 | 欧美同性gayvidoes | 超碰av | 视频精品一区二区三区 | 亚洲高清毛片一区二区 | 国产亚洲精品高清在线 | 亚洲29p | 美女的隐私脱裤子无遮挡 | 欧美日韩久久中文字幕 | 九九热这里只有精品2 | 国内精品 大秀视频 日韩精品 | 免费看60分钟大片视频播放 | 免费看的毛片 | 精东影业传媒全部作品 | 黑人巨| 国产女乱淫真高清免费视频 | 天美蜜桃精东乌鸦传媒 | 国产91无毒不卡在线观看 | 挑色视频| 俄罗斯12一15处交 | 日韩无砖专区2020在线 | xxxxxx性受 |