首页 > 其他分享 >SprigMvc文件下载

SprigMvc文件下载

时间:2023-11-10 14:22:50浏览次数:23  
标签:文件 inputStream filename file new SprigMvc httpHeaders 下载

@RequestMapping(method = RequestMethod.GET ,value = "/down")
public ResponseEntity<byte[]> DownLoad(HttpServletRequest request,String filename ){

// 获取文件的真实路径
String realPath = request.getServletContext().getRealPath("/WEB-INF/download/" + filename);
HttpHeaders httpHeaders ;
byte[] file ;
try {
InputStream inputStream=new FileInputStream(realPath);
try {
httpHeaders=new HttpHeaders();
file =new byte[inputStream.available()];
inputStream.read(file);
httpHeaders.add("Content-Disposition","attachment;filename="+filename);
httpHeaders.setContentDispositionFormData("Content-Disposition", URLEncoder.encode(filename, "utf-8"));
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}

//需要传入的三个参数 文件名 文件的的字节读入数组 文件的请求头 文件的状态
ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(file, httpHeaders, HttpStatus.OK);
return responseEntity;

}

标签:文件,inputStream,filename,file,new,SprigMvc,httpHeaders,下载
From: https://www.cnblogs.com/fangyilovebc/p/17824015.html

相关文章

  • CentOS系统,文件压缩与解压命令大全
    在CentOS系统中,可以使用多种命令进行文件压缩和解压缩操作。以下是常见的文件压缩和解压命令及其用法:一、tar:用于打包文件或目录,并可选择压缩为tar压缩包1.创建tar压缩包:tar-cvfarchive.tarfile1file2directory2.解压他让压缩包:tar-xvfarchive.tar3.创建tar.gz压缩......
  • IDEA中配置配置国际化时,properties文件没有Resource Bundle如何解决
    如果IDEA中配置国际化时,.properties文件没有可视化界面,则需要安装一个ResourceBundleEditor的插件 ......
  • mongodb等号格式的配置文件转换成yaml格式的配置文件
    环境:mongodb:4.4.22 1.等号格式的配置文件[root@localhostconf]#moremongo.cnfport=28001fork=truedbpath=/home/middle/mongodb/datalogpath=/home/middle/mongodb/log/mongodb.logpidfilepath=/home/middle/mongodb/run/28001.pidlogappend=truebind_ip=192.1......
  • 基于Resources Saver 下载网页信息
    1.打开Chrome应用商店https://chrome.google.com/webstore/search/resources%20saver?utm_source=ext_app_menu&_category=extensions 2.搜索 ResourcesSaver点进去 【添加至Chrome】3.打开浏览器控制台找到  ResourcesSaver菜单栏点击SaveAll  Resources 即可......
  • python读写文件
    str="Hello,World!"#打开一个文件,如果文件不存在,它将被创建file=open("my_file.txt","w")#将字符串写入文件file.write(str)#关闭文件file.close()#重新打开文件以读取内容file=open("my_file.txt","r")content=file.read()file.clos......
  • 通过运行中的容器生成 Docker Compose 配置文件
    背景笔者之前有一次不小心删除了原始的docker-compose.yml文件,不过正在运行的Docker容器还在,找了许久,发现一个方法可以从这些容器中生成一个等效的DockerCompose配置文件。本文将介绍使用autocompose工具从正在运行的容器中反向生成docker-compose.yml文件。安装所需工......
  • Kubernetes常用命令及yml文件、集群网络 Kubernetes组件介绍及环境搭建
    Kubernetes常用命令及yml文件、集群网络Kubernetes组件介绍及环境搭建Kubernetes组件介绍及环境搭建一、kubernetes常用命令说明:因为k8s的命令都是通过kubectl组件接收的,这个组件只在master节点有,所以k8s的命令都是在master节点中执行kubectlgetnodes#查看当前集群中......
  • C++函数分文件编写
    1、创建.h后缀名的头文件并且声明需要调用的函数,并且给出通用输出流和输出流#include<iostream>usingnamespacestd;voidasd();2、创建.cpp后缀名的源文件并且调用刚刚定义的头文件#include"asd.h"voidasd(){ cout<<"HelloWorld!"<<endl;}3、在源文件中......
  • Linux文件处理命令
    概述场景引入分布式系统面临的配置问题微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了Confi......
  • apache httpd文件服务器(三)
    创建file文件夹,生成txt测试文件[root@VM-0-5-centoshtml]#echo"abc333">file/b.txt[root@VM-0-5-centoshtml]#lsfile/a.txtb.txt创建exe文件[root@VM-0-5-centoshtml]#touchfile/a.exe[root@VM-0-5-centoshtml]#lsfile/a.exea.txtb.txt查看log访问记录vi/......