本文实例为大家分享了SpringMVC文件下载的具体代码,供大家参考,具体内容如下
两个案例
1.为登录用户提供下载服务。
2.阻止仅通过输入网址即可获取下载。
文件下载概览
为了将文件发送给浏览器,我们需要在控制器中完成以下操作:
- 对请求处理方法使用void返回类型,并且在方法中添加HttpServletResponse参数。
- 将响应的内容类型设为文件的内容类型。Content-Type标题在某个实体的body中定义数据的类型,并包含媒体类型和子类型标识符。如果不清楚内容类型,并且希望浏览器失始终显示保存对话框,则将它设为APPLICATION/OCTET-STREAM。这个值时不区分大小写的。
- 添加一个名为Content-Disposition的HTTP响应标题,并赋值attachment;filename=fileName,这里的fileName是默认的文件名。
案例1:为登录用户提供下载服务
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>点击链接下载文件</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:阻止仅通过输入网址即可获取下载
?
|
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来浏览器输入网址就能下载图片的情况
{
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;
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 90
-
2025-06-04 32
-
2025-05-25 50
-
2025-05-25 31
-
2025-05-29 89
热门评论

