当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

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

SOC项目AHB_SD_HOST控制器设计

Gazebo installation, uninstall and upgrade

Go installation and configuration (1)

【我的OpenGL学习进阶之旅】关于OpenGL的着色器的向量和矩阵分类的访问方式: xyzw/rgba/stpq以及数组下标

It turns out that you are such an array. You have finally learned

InnoDB engine in MySQL

经纬恒润再次荣获PACCAR集团 10PPM 质量奖

汇编语言学习一(有栈协程铺垫,32位寄存器和相关指令学习,未完待续06/29)
![[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network](/img/39/c9bf235828b6d50e7931876fa4a918.png)
[fuzzy neural network] mobile robot path planning based on Fuzzy Neural Network
随机推荐
RT thread migration to s5p4418 (III): static memory pool management
What if I forget my account number after opening an account? Is it safe to open an account online?
第一行代码(第三版)学习笔记
图解八股,真的太顶了
ROS program compilation, like no compilation, refers to the execution of the old compiled executable program
RT thread migration to s5p4418 (IV): thread synchronization
leetcode:98. Validate binary search tree
1.8 - 多级存储
Record one time of Tencent Test Development Engineer's automation interface test practice experience
Finished product upgrade procedure
gazebo/set_ model_ State topic driving UAV model through posture
It turns out that you are such an array. You have finally learned
SOC_ SD_ CLK
1.4 - fixed and floating point numbers
Gazebo installation, uninstall and upgrade
成品升级程序
SOC项目AHB_SD_HOST控制器设计
Joseph problem C language
Rising posture series: fancy debugging information
判断h5在两端是在微信环境还是企业微信环境