SpringBoot/SpringMVC 下载本地文件
页面代码:
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>标题</title>
</head>
<body>
<h2>It works!</h2>
<a href="/downloadLocalXlsxFile">Local xlsx file download</a>
</body>
</html>
<script type="text/javascript">
<!--
// 脚本
//-->
</script>
后台代码:
package com.example.demo;
import java.io.FileInputStream;
import java.net.URLDecoder;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@SpringBootApplication
public class SpringBootWeb1Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootWeb1Application.class, args);
}
@RequestMapping("/")
public String index() {
return "index.html";
}
@RequestMapping("/downloadLocalXlsxFile")
public void downloadLocalXlsxFile(HttpServletResponse res, HttpServletRequest req) throws Exception {
String localFilename = "Book1.xlsx";
String localFilepath = getClass().getResource("/templates/" + localFilename).getPath();
res.setContentType("multipart/form-data");
res.setCharacterEncoding("UTF-8");
res.setContentType("text/html");
String userAgent = req.getHeader("User-Agent");
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
// IE Core
localFilename = java.net.URLEncoder.encode(localFilename, "UTF-8");
} else {
// Non-IE Core
localFilename = new String((localFilename).getBytes("UTF-8"), "ISO-8859-1");
}
res.setHeader("Content-Disposition", "attachment;fileName=" + localFilename);
localFilepath = URLDecoder.decode(localFilepath, "UTF-8");
FileInputStream instream = new FileInputStream(localFilepath);
ServletOutputStream outstream = res.getOutputStream();
int b = 0;
byte[] buffer = new byte[1024];
while ((b = instream.read(buffer)) != -1) {
outstream.write(buffer, 0, b);
}
instream.close();
if (outstream != null) {
outstream.flush();
outstream.close();
}
}
}
SpringBoot/SpringMVC 下载本地文件的更多相关文章
- flask 下载本地文件
下载本地文件就是找到文件路径 调用flask自带的send_file(路径)下载, 并返回 flask: # 下载文件 from flask import send_file@task_mgm.ro ...
- SSH上传/下载本地文件到linux服务器
在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件 scp username@servername:/path/filename /var/www/local_dir(本 ...
- 如何从TFS(Visual Studio Team Foundation Server)映射下载本地文件夹
1.连接tfs项目 首先打开vs2017 ——>工具栏 中的 团队——> 选择团队的管理链接 2.选择管理工作区 显示管理工作区的弹窗,点击 编辑 显示弹窗,选择本地文件夹(即要保存 ...
- XShell上传、下载本地文件到linux服务器
Python之道发表于程序员八阿哥订阅 1.2K 腾讯云服务器 年付3折起 首次购买云服务器 最低3折起 超高性价比 限时抢购 Xshell很好用,然后有时候想在windows和linux上传或下载某 ...
- linux学习 XShell上传、下载本地文件到linux服务器
(一)通过命令行的方式 1.linux服务器端设置 在linux主机上,安装上传下载工具包rz及sz; 如果不知道你要安装包的具体名称,可以使用yum provides */name 进行查找系统自带 ...
- java web 下载本地文件并弹出下载框
window.open("/dept/download") jsp代码 @RequestMapping(value = "/download", method ...
- [SpringBoot/SpringMVC]从Webapp下载一个大文件出现java.lang.OutOfMemoryError: GC overhead limit exceeded怎么办?
本文示例工程下载:https://files.cnblogs.com/files/xiandedanteng/WebFileDownload20191026.rar 制作一个Webapp,让其中一个网 ...
- Java传统下载和SpringMVC下载
/** * 传统下载 * @param filename 文件名称 */ @RequestMapping(value="download", method={RequestMeth ...
- SpringMVC文件上传下载(单文件、多文件)
前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...
随机推荐
- yml格式的在线转换工具
工具1: https://www.toolfk.com/tool-format-yaml 工具2: https://www.toyaml.com/index.html https://www.cnbl ...
- Linux命令——cp、rm、mv、touch、file、dir
cp copy 拷贝文件 拷贝过程不指定目标文件名 则目标文件名和源文件名一样 [root@WebServer ~]# cp /91xueit/teacher.txt 51cto/ 拷贝过程指定目标文 ...
- 【Intel 汇编】ELF文件
ELF文件格式是一个开放标准,各种UNIX系统的可执行文件都采用ELF格式,它有三种不同的类型: 可重定位的目标文件(Relocatable,或者Object File) 可执行文件(Executab ...
- 网页报警提示 This page includes a password or credit card input in a non-secure context. A warning has been added to the URL bar. For more information, see https://goo.gl/zmWq3m.
This page includes a password or credit card input in a non-secure context. A warning has been added ...
- 【基础数位DP-模板】HDU-2089-不要62
不要62 Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Su ...
- Jsp与JavaScript区别
有时候会误以为这两个是同一个概念,但其实不是 Jsp全名为Java Server Pages(Java服务器页面),其根本是一个简化的Servlet设计,他实现了在Java当中使用HTML标签.Jsp ...
- redis事务机制
目录 一.事务的实现 1.multi——开启事务 2.命令入队列 3.exec——执行事务 4.DISCARD——放弃执行 5.错误处理 二.watch命令 redis官方文档:Redis trans ...
- JMeter学习之元件的作用域与执行顺序
1.元件的作用域 JMeter中共有8类可被执行的元件(测试计划与线程组不属于元件),这些元件中,取样器是典型的不与其它元件发生交互作用的元件,逻辑控制器只对其子节点的取样器有效,而其它元件(conf ...
- mongodb中find $ne null 与$exists的区别
$ne null 会把空列表也算入,即使不存在. $exists 的识别效果就比较好 1.插入样例数据 db.nullexistsdemo.insertMany( [{ "name" ...
- linux下替换不同目录下的文件的字符串
需求: 查找当前目录下的所有子目录中的catalina.sh文件中的JAVA_OPTS=开头的文件,替换为 JAVA_OPTS='-server -Xms800m -Xmx800m -XX:PermS ...