SpringMVC实现文件下载功能

网友投稿 352 2023-06-04


SpringMVC实现文件下载功能

本文实例为大家分享了SpringMVC文件下载的具体代码,供大家参考,具体内容如下

两个案例

1.为登录用户提供下载服务。

2.阻止仅通过输入网址即可获取下载。

文件下载概览

为了将文件发送给浏览器,我们需要在控制器中完成以下操作:

对请求处理方法使用void返回类型,并且在方法中添加HttpServletResponse参数。

将响应的内容类型设为文件的内容类型。Content-Type标题在某个实体的body中定义数据的类型,并包含媒体类型和子类型标识符。如果不清楚内容类型,并且希望浏览器失始终显示保存对话框,则将它设为APPLICATION/OCTET-STREAM。这个值时不区分大小写的。

添加一个名为Content-Disposition的HTTP响应标题,并赋值attachment;filename=fileName,这里的fileName是默认的文件名。

案例1:为登录用户提供下载服务

Domain类

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控制器

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, HttpServletResponshttp://e 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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

Down

LoginForm.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

账号:


密码:


案例2:阻止仅通过输入网址即可获取下载

@RequestMapping(value = "/resource_download")

public String downloadResource(HttpSession session, HttpServletRequest request, Hhttp://ttpServletResponse 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;

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ng2学习笔记之bootstrap中的component使用教程
下一篇:react实现pure render时bind(this)隐患需注意!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~