SpringMVC实现文件下载功能
程序员文章站
2024-03-04 14:18:23
本文实例为大家分享了springmvc文件下载的具体代码,供大家参考,具体内容如下
两个案例
1.为登录用户提供下载服务。
2.阻止仅通过输入网址即可获取下载...
本文实例为大家分享了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, 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
<%@ 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
<%@ 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:阻止仅通过输入网址即可获取下载
@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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。