当前位置:网站首页>不依赖框架的文件下载
不依赖框架的文件下载
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中使用,不同框架不同。边栏推荐
- 二进制到汇编:进制,原码反码补码,位运算,通用寄存器,内存一套打通
- Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
- pdf和word等文档添加水印
- xxx is not in the sudoers file.This incident will be reported error
- 无法完成包的安装npm ERR! Refusing to install package with name “moment“ under a package also called “moment“
- 迷宫问题----经典回溯法解决
- 爬2.12.6的Antd上传图片坑
- 51数码管显示
- IO进程线程->目录IO->day3
- 基于STM32F103的消防系统之驱动电机风扇
猜你喜欢

QT Weekly Skills (1) ~~~~~~~~~ Running Icon

51数码管显示

Unity Gizmos扩展:线框圆

主机和从机配置,建立ssh连接实现Rviz远程控制

IEEE在指定期刊下搜索相关论文

Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"

VSCode隐藏左边活动栏

使用Dva项目作Antd的Demo
Three working modes of CPU: real mode, protected mode, long mode

The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
随机推荐
主机和从机配置,建立ssh连接实现Rviz远程控制
独立按键控制led
Diwen serial screen production (serialization 1) ===== preparation work
闭包(你不知道的JS)
动态规划进阶 JS
c语言编程练习
删除当前路径下含某个关键字的所有文件
xxx is not in the sudoers file.This incident will be reported error
工程师必看:常见的PCB检测方法有哪些?
ssh script space character conversion
关于 PCB 多层板制程能力不得不说的那些事儿
VsCode连接远程服务器并修改文件代码
51数码管显示
Written before the official account - QT, ARM, DSP, microcontroller, power electronics and transmission!
Three working modes of CPU: real mode, protected mode, long mode
顺序二叉树---实现数组的二叉树前序遍历输出
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
阿里京东“喜提”国电投,顶流互联网和能源大厂为何合作?
sizeof和strlen最全区别,以及指针和数组运算解析
-----博客声明