vue+springboot文件下载
//vue element-ui
//后台java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DownLoadFile {
private static Logger log = LoggerFactory.getLogger(DownLoadFile.class);
@RequestMapping(value = "/downLoad", method = RequestMethod.GET)
public static final String downLoad(HttpServletResponse res) throws UnsupportedEncodingException {
Map<String, Object> reMap = new HashMap<>();
//文件名 可以通过形参传进来
String fileName = "t_label.txt";
//要下载的文件地址 可以通过形参传进来
String filepath = "f:/svs/" + fileName;
OutputStream os = null;
InputStream is = null;
try {
// 取得输出流
os = res.getOutputStream();
// 清空输出流
res.reset();
res.setContentType("application/x-download;charset=GBK");
res.setHeader("Content-Disposition",
"attachment;filename=" + new String(fileName.getBytes("utf-8"), "iso-8859-1"));
// 读取流
File f = new File(filepath);
is = new FileInputStream(f);
if (is == null) {
reMap.put("msg", "下载附件失败");
}
// 复制
IOUtils.copy(is, res.getOutputStream());
res.getOutputStream().flush();
} catch (IOException e) {
reMap.put("msg", "下载附件失败,error:" + e.getMessage());
}
// 文件的关闭放在finally中
finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
log.error(e.toString());
}
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
log.error(e.toString());
}
}
String str = JsonUtil.map2Json(reMap);
return str;
}
}
说明:此代码用火狐浏览器测试的时候没问题,谷歌版本以及设置不一样可能会出现文件下载错误,甚至不直接弹出另存为的对话框
vue+springboot文件下载的更多相关文章
- 对Web(Springboot + Vue)实现文件下载功能的改进
此为 软件开发与创新 课程的作业 对已有项目(非本人)阅读分析 找出软件尚存缺陷 改进其软件做二次开发 整理成一份博客 原项目简介 本篇博客所分析的项目来自于 ジ绯色月下ぎ--vue+axios+sp ...
- vue+springboot前后端分离实现单点登录跨域问题处理
最近在做一个后台管理系统,前端是用时下火热的vue.js,后台是基于springboot的.因为后台系统没有登录功能,但是公司要求统一登录,登录认证统一使用.net项目组的认证系统.那就意味着做单点登 ...
- docker-compose 部署 Vue+SpringBoot 前后端分离项目
一.前言 本文将通过docker-compose来部署前端Vue项目到Nginx中,和运行后端SpringBoot项目 服务器基本环境: CentOS7.3 Dokcer MySQL 二.docker ...
- vue+springboot上传和下载附件功能
https://blog.csdn.net/qq_35867245/article/details/84325385 上传附件(服务端代码) 第一步:在application.yml中配置附件要上传的 ...
- vue springboot利用easypoi实现简单导出
vue springboot利用easypoi实现简单导出 前言 一.easypoi是什么? 二.使用步骤 1.传送门 2.前端vue 3.后端springboot 3.1编写实体类(我这里是dto, ...
- 优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download
优化vue+springboot项目页面响应时间:waiting(TTFB) 及content Download TTFB全称Time To First Byte,是指网络请求被发起到从服务器接收到地 ...
- 使用VUE+SpringBoot+EasyExcel 整合导入导出数据
使用VUE+SpringBoot+EasyExcel 整合导入导出数据 创建一个普通的maven项目即可 项目目录结构 1 前端 存放在resources/static 下 index.html &l ...
- vue+axios+springboot文件下载
//前台代码 <el-button size="medium" type="primary" @click="downloadFile" ...
- Vue+SpringBoot+Mybatis的简单员工管理项目
本文项目参考自:https://github.com/boylegu/SpringBoot-vue 为了完成此项目你需要会springBoot,mybatis的一些基本操作 运行界面 第一步:搭建前端 ...
随机推荐
- 硬件对同步的支持-TAS和CAS指令
目录 Test and Set Compare and Swap 使用CAS实现线程安全的数据结构. 现在主流的多处理器架构都在硬件水平上提供了对并发同步的支持. 今天我们讨论两个很重要的硬件同步指令 ...
- egret canvas的style
<canvas width="1920" height="1080" style=" cursor:auto;//鼠标样式 positon:ob ...
- JavaScript基础函数的配置对象Configuration Objects(020)
配置对象通常用在API库的实现中,当程序中需要编写要多次的模块,也可以采用这种模式.这种模式的好处是接口明确,扩展方便.比如,一个 addPerson在设计的最初需要两个参数作为初始化时人的姓名: f ...
- 安装pymysql模块及使用
安装pymysql模块: https://www.cnblogs.com/Eva-J/articles/9772614.html file--settings for New Projects---P ...
- 11. RobotFramework内置库-Collections
Collections库是RobotFramework用来处理列表和字典的库,详细可参见官方介绍. 官方地址:http://robotframework.org/robotframework/late ...
- css中 出现height为100%失效的原因及解决方案
我们都知道需要给html和body标签设置了高度height:100%之后,再给内部的div设置height:100%的时候,内部div的高度100%才会起到作用.这是由于:%是一个相对父元素计算得来 ...
- linux系统配置常用命令top
本人测试系统:centos7 命令名称:top Linux top命令用于实时显示 process 的动态. 参数:-b 批处理 -c 显示完整的治命令 -I 忽略失效过程 -s 保密模式 -S 累积 ...
- Mysql中交换行操作
博客已搬家,更多内容查看https://liangyongrui.github.io/ Mysql中交换行操作 leetcode的一道题目 参考:https://leetcode.com/proble ...
- 开源API文档工具- swagger2 与 smart-doc 比较 与 使用
工具开源地址 swagger2 : https://swagger.io/ smart-doc: https://www.oschina.net/p/smart-doc 国产 两者的比较 swagg ...
- Django之重写用户模型
django——重写用户模型 Django内建的User模型可能不适合某些类型的项目.例如,在某些网站上使用邮件地址而不是用户名作为身份的标识可能更合理. 1.修改配置文件,覆盖默认的User模型 D ...