当前位置:网站首页>Browser downloads files as attachments
Browser downloads files as attachments
2022-06-30 06:51:00 【Sunny husband】
@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. Get request parameters , File name
String filename = request.getParameter("filename");
//2. Use byte input stream to load file into memory
//2.1 Find the file server path
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/img/" + filename);
//2.2 Associate with a byte stream
FileInputStream fis = new FileInputStream(realPath);
//3. Set up response Response header for
//3.1 Set response header type :content-type
String mimeType = servletContext.getMimeType(filename);// retrievable mime type
response.setHeader("content-type",mimeType);
//3.2 Set the opening mode of response header :content-disposition
// Solve the problem of Chinese file name
//1. obtain user-agent Request header 、
String agent = request.getHeader("user-agent");
//2. Use the tool class method to encode the file name
filename = getFileName(agent, filename);
response.setHeader("content-disposition","attachment;filename="+filename);
//4. Write the data from the input stream into the output stream
ServletOutputStream sos = response.getOutputStream();
byte[] bytes= new byte[1024 * 8];
int len = 0;
while((len = fis.read(bytes)) != -1){
sos.write(bytes,0,len);
}
fis.close();
}
// Get the Chinese file name of different browser attachments
public String getFileName(String agent, String filename) throws UnsupportedEncodingException {
if (agent.contains("MSIE")) {
// IE browser
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
} else if (agent.contains("Firefox")) {
// Firefox
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
} else {
// Other browsers
filename = URLEncoder.encode(filename, "utf-8");
}
return filename;
}
边栏推荐
- 1.4 - 定点数与浮点数
- Four tips in numpy
- Performance comparison of random network, scale-free network, small world network and NS small world matlab simulation
- Why does ETL often become ELT or even let?
- The solution of memcpy memory overlap
- SOC_AHB_SD_IF
- 【Mask-RCNN】基于Mask-RCNN的目标检测和识别
- leetcode:98. 验证二叉搜索树
- 1.6 - CPU组成
- 第一行代码(第三版)学习笔记
猜你喜欢

Introduction to neural networks

Analysis of startup process of gazebo multi computer simulation

基础刷题(一)

原来你是这样的数组,终于学会了

【模糊神经网络】基于模糊神经网络的移动机器人路径规划

0 basic job transfer software test, how to achieve a monthly salary of 9.5k+

与MQTT的初定情缘

Fastapi learning Day1

Principle: webmvcconfigurer and webmvcconfigurationsupport pit avoidance Guide

MySQL优化:从十几秒优化到三百毫秒
随机推荐
Getting started with research
oracle
【Mask-RCNN】基于Mask-RCNN的目标检测和识别
1.7 - CPU的性能指标
【我的OpenGL学习进阶之旅】关于OpenGL的着色器的向量和矩阵分类的访问方式: xyzw/rgba/stpq以及数组下标
【Hot100】15. 三数之和
神经网络入门
Principle: webmvcconfigurer and webmvcconfigurationsupport pit avoidance Guide
tomorrow! "Mobile cloud Cup" competition air publicity will start!
The 40g high-efficiency cloud disk purchased by Alibaba cloud is only 20g attached
华泰炒股安全吗?我想网上开户。
Go installation and configuration (1)
写一个C程序判断系统是大端字节序还是小端字节序
leetcode:98. 验证二叉搜索树
Collections tool class (V)
随机网络,无标度网络,小世界网络以及NS小世界的性能对比matlab仿真
1.2(补充)
ROS-URDF
Four ways to create multithreads
与MQTT的初定情缘