当前位置:网站首页>不依赖框架的文件下载
不依赖框架的文件下载
2022-07-30 05:47:00 【杰拉拉德】
package com.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @ClassName: FileUtils.java
*
* @Description: 文件下载
*
* @author GERRARD
*
* @date 2015年1月27日下午3:10:10
*
*/
public final class FileUtils {
/**
* 文件下载
*
* @param request
* @param response
* @param filePath
* 文件相对路径(带文件名和后缀)
* @throws IOException
*/
public static void downloadFile(HttpServletRequest request,
HttpServletResponse response, String filePath) throws IOException {
// 下载文件所在路径
String basePath = request.getSession().getServletContext()
.getRealPath("/");
String downloadPath = basePath + filePath;
File fn = new File(downloadPath);
// 设置下载文件名
String fileName = fn.getName();
// 读取下载文件流
InputStream inStream = new FileInputStream(downloadPath);
// 设置输出的格式
response.reset();
response.setContentType("APPLICATION/OCTET-STREAM");
fileName = URLEncoder.encode(fileName, "utf-8");
response.setHeader("Content-Disposition",
"attachment; filename=\"" + fileName + "\"");
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
inStream.close();
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
inStream.close();
response.getOutputStream().close();
}
}
}
Action中调用
FileUtils.downloadFile(ServletActionContext.getRequest(),ServletActionContext.getResponse(), "/WEB-INF/model/template/"+ fileName);
其中 ServletActionContext.getRequest(),ServletActionContext.getResponse()是struts2中使用,不同框架不同。边栏推荐
- js 替换字符串中所有 “ 引号 —— 数据处理
- IO进程线程->目录IO->day3
- 【markdown常用用法】
- openssl 1.1.1 compile statement
- [Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]
- 四、6、前缀、中缀、后缀表达式(逆波兰表达式)
- OpenLayers (ol包),Vite显示地图(附源码)
- 独立按键控制led进阶(1)
- js高级学习笔记(详细)
- 爬2.12.6的Antd上传图片坑
猜你喜欢
查看 word版本号
图扑软件携手华为云再创合作共赢新局面
牛顿迭代法求方程的根
js高级学习笔记(详细)
[Quick MSP430f149] Notes on learning MSP430f149 during the game
【已解决:el-input标签无法输入或不显示文字】
Unity Gizmos扩展:线框圆
VSCode隐藏左边活动栏
vscode set sublime theme
Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
随机推荐
给Vscode配置ESlint语法检查 — ESLint 插件自动格式化设置(实现Ctrl+S 按照ESLint规则自动格式化代码)
单片机之流水灯
IEEE在指定期刊下搜索相关论文
【部分项目展示】
Unity 渲染流水线
NS3 error fatal error: ns3/opengym-module.h: No such file or directory
xxx is not in the sudoers file.This incident will be reported error
工程师必看:常见的PCB检测方法有哪些?
洛谷一P1097 [NOIP2007 提高组] 统计数字
Unity Shader 标准光照模型——漫反射
---------手撕二叉树,完成二叉树的前中后序遍历,以及前中后序查找
独立按键控制led进阶(1)
lcd1602调试
FPGA parsing B code----serial 2
FPGA parsing B code----serial 1
antd table Summary总结栏置顶
[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]
NS3报错 fatal error: ns3/opengym-module.h: No such file or directory
图扑软件携手华为云再创合作共赢新局面
使用Dva项目作Antd的Demo