[DEBUG] Spring boot前端html无法下载示例文件
更新:原方法打jar包的时候是可以的,后来我打war包之后下载的文件就是0字节。尴尬:)
所以现在更换一种方法,然后打war包。在服务器已测试成功。
前端不需要改变,只需要更改controller:
@RequestMapping("/example")
@ResponseBody
public void downloadExampleFileForUser(HttpServletResponse response) throws Exception {
String filePath = "/static/example.txt";
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
downloadExampleFile(response, filePath, fileName);
return;
}
private void downloadExampleFile(HttpServletResponse response, String path, String name) throws IOException {
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
response.setContentType("content-type:octet-stream");
// FileUploadController是当前文件的名称
InputStream inputStream =FileUploadController.class.getResourceAsStream(path);
OutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
}
然后把要下载的文件,example.txt放在static下

=======================================================================
我只想在前端给一个示例文件example.zip供下载!静态的,一般不改变。
在html中写下载链接,但是始终报无法下载。
已经尝试过:
1.把example.zip拷贝在resources根目录下和templates下
2。前端删掉href或者download都不对
<a id="example_file" href="example.zip" download="example.zip" >Example File</a>
网上有一种思路是重写输出,但是我懒,所以用了简单点的方法。
============================================================================
把example.zip放在templates下
html修改:
<a id="example_file" th:href="@{/example}" download="example.zip" >Example File</a>
controller修改
@RequestMapping("/example")
public String downloadExampleFile(HttpServletRequest request, HttpServletResponse response) {
String fileName = "example.zip";
//System.out.println();
File file = new File(fileName);
if (file.exists()) {
System.out.println(file.getParent());
}
return null;
}
其实啥都没做。。。。就是给了个映射==
===============================================
我是Ruriko,我爱这个世界:)
[DEBUG] Spring boot前端html无法下载示例文件的更多相关文章
- 【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
编写Java Spring Boot应用,通过配置logging.path路径把日志输出在指定的文件夹中. 第一步:通过VS Code创建一个空的Spring Boot项目 第二步:在applicat ...
- Spring boot 国际化自动加载资源文件问题
Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...
- 基于spring boot 2.x的websocket示例
spring boot 2/spring 5自带了websocket,下面是最基本的示例(包括java服务端.java客户端以及js客户端) 一.pom依赖 <dependencies> ...
- Android+Spring Boot 选择+上传+下载文件
2021.02.03更新 1 概述 前端Android,上传与下载文件,使用OkHttp处理请求,后端使用Spring Boot,处理Android发送来的上传与下载请求.这个其实不难,就是特别多奇奇 ...
- 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)
1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...
- 后端Spring Boot+前端Android交互+MySQL增删查改
2021.1.27 更新 已更新新版本博客,更新内容很多,因此新开了一篇博客,戳这里. 1 概述 使用spring boot作为后端框架与Android端配合mysql进行基本的交互,包含了最基本的增 ...
- Spring boot 读取resource目录下的文件
背景:最近做项目重构将以前的ssh + angular js架构,重构为spring boot + vue.项目是一个数据管理平台,后台涉及到多表关联查询,数据导入导出等. 问题:读取resource ...
- Spring Boot 集成阿里云 OSS 进行文件存储
最近因为项目中需要存储很多的图片,不想存储到服务器上,因此就直接选用阿里云的对象服务(Object Storage Service,简称 OSS)来进行存储,本文将介绍 Spring Boot 集成 ...
- spring boot:用itextpdf处理pdf表格文件(spring boot 2.3.2)
一,什么是itextpdf? 1,itextpdf的用途 itextpdf是用来生成PDF文档的一个java类库, 通过iText可以生成PDF文档, 还可以把XML/Html文件转化为PDF文件 2 ...
随机推荐
- 内存管理4-Autoreleasepool
自动释放池是OC里面的一种内存回收机制,一般可以将一些临时变量添加到自动释放池中,统一回收释放,当自动释放池销毁时,池里面的所有对象都会调用一次release,也就是计数器会减1,但是自动释放池被销毁 ...
- 高逼格Linux命令,忙的飞起
以mac为例,先安装Homebrew 第一个命令:sl 安装命令:brew install sl 运行:sl 效果:小火车从右向左跑起来,污污污 第二个命令:cmatrix 安装命令:brew ins ...
- Jenkins 插件加速下载
本文主旨 看到好多加速Jenkins安装插件速度的文章,大多数教程中都是在插件配置里使用 https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/up ...
- 启动Docker后台容器,测试运行Hello-world
1.使用docker run hello-world命令运行测试hello-world镜像,如下: 2.run 命令都干了什么?如下图: 3.为什么Docker比vm快?
- @Configuration,@ConfigurationProperties,@EnableConfigurationProperties
@Configuration API: https://www.javadoc.io/doc/org.springframework/spring-context/5.0.7.RELEASE @Con ...
- zabbix :web 界面显示的监控项值为0或者空
[参考文章]:[错误汇总]zabbix_get 的值一直为 0 1. 问题现象 zabbix 版本:3.4: server 端部署在 192.168.145.134 ,agent 节点部署在 192. ...
- Swift 可选(Optionals)类型
Swift 的可选(Optional)类型,用于处理值缺失的情况.可选表示"那儿有一个值,并且它等于 x "或者"那儿没有值". Swfit语言定义后缀?作为命 ...
- [Scikit-learn] 2.3 Clustering - Spectral clustering
From: 2.3.5 Clustering - Spectral clustering From: 漫谈 Clustering (4): Spectral Clustering From: 漫谈 C ...
- Ideal 报错之 Class ** is never used 解决办法
错误信息: 解决办法: file ------setting ---------inspections----------Groovy--------------Unuse Declaration ...
- 用Keras搭建神经网络 简单模版(五)——RNN LSTM Regressor 循环神经网络
# -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) import matplotlib.pyplot as plt from ...