Spring MVC中下载文件
来源:互联网 发布:大数据平台架构师 编辑:程序博客网 时间:2024/05/17 07:49
1、简单的
直接在页面中加a标签
<a href="../resource/download/EBeansAndroid.apk"></a>2、复杂的
写后台代码
@RequestMapping(value = "/downloadApk", method = RequestMethod.POST)@ResponseBodypublic Map<String,Object> downloadApk(HttpServletRequest request,HttpServletResponse response){//获取网站部署路径(通过ServletContext对象),用于确定下载文件位置,从而实现下载 String path = request.getRealPath("/"); //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType("multipart/form-data"); //2.设置文件头:最后一个参数是设置下载文件名 response.setHeader("Content-Disposition", "attachment;fileName="+"aaa.apk"); ServletOutputStream out; //通过文件路径获得File对象 File file = new File(path + "resource\\download\\" + "aaa.apk"); try { FileInputStream inputStream = new FileInputStream(file); //3.通过response获取ServletOutputStream对象(out) out = response.getOutputStream(); int b = 0; byte[] buffer = new byte[1024];//[]中为1024的倍数即可 while (b != -1){ b = inputStream.read(buffer); //4.写到输出流(out)中 if(b-1>=0){//防止数组越界 out.write(buffer,0,b); } } inputStream.close(); out.close(); out.flush(); } catch (IOException e) { e.printStackTrace(); }}
0 0
- Spring MVC中下载文件
- spring中下载文件
- tomcat 中下载文件
- Form中下载文件
- JSP中下载文件
- ASP.NET中下载文件
- 从链接中下载文件
- 微信中下载文件
- 从url中下载文件
- Windows中下载文件时的问题
- delphi程序中下载文件的问题
- 如何在ASP.NET中下载文件
- 如何在 ASP.NET 中下载文件
- 如何在ASP.NET中下载文件
- 如何在ASP.NET中下载文件
- 如何在ASP.NET中下载文件
- 如何在ASP.NET中下载文件
- 如何在ASP.NET中下载文件
- 游承超:防爆膜如何挑选?(16P)
- 仿qq弹出列表的actionSheet用oc ,swift2和安卓的简单实现
- Wifidog Commandline.c&.h分析
- [linux][node]软连接建立
- Application context和Activity context的区别
- Spring MVC中下载文件
- golang的小总结
- 欢迎使用CSDN-markdown编辑器
- 一位软件工程师的6年总结
- 使用android studio导入之前版本系统的project sync failed 解决办法
- 杭电 1015 Safecracker
- python tips - 注意函数参数的默认值-默认参数-可选参数
- QML之javascript
- 尼泊尔部分地方成"孤岛" 死亡人数或过万