当前位置:网站首页>Servlet response download file
Servlet response download file
2022-06-26 00:13:00 【Daily log of the great demon king】
package com.wlt.response.c.body;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
/**
* File download
*/
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
String filename = request.getParameter(" ");
ServletContext s = this.getServletContext();
// Set up mine
String mt = s.getMimeType(filename);
response.setContentType(mt);
// Set download header information
response.setHeader("content-disposition", "attchment;filename="+filename);
// Cross reference flow
// Get input stream
InputStream is = s.getResourceAsStream("/download/"+filename);
// Get the output stream
ServletOutputStream os = response.getOutputStream();
/* The first one is
int len=-1;
byte[] b=new byte[1024];
while ((len=is.read(b))!=-1) {
os.write(b,0,len);
}*/
// The second kind : Using the methods of tool classes commons-io-1.4.rar
IOUtils.copy(is, os);
// Shut off flow
os.close();
is.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
边栏推荐
- Simulation connection between WinCC and STEP7_ Old bear passing by_ Sina blog
- 网络协议之:redis protocol详解
- Redis之常见问题
- Keil compilation run error, missing error: # 5: # includecore_ cm3.h_ Old bear passing by_ Sina blog
- POSTMAN测试出现SSL无响应
- Explain the synchronize keyword
- 迅为RK3568开发板使用RKNN-Toolkit-lite2运行测试程序
- Given the parameter n, there will be n integers 1, 2, 3,... From 1 to n, n. These n arrays have n! An arrangement that lists all columns in ascending order of size and marks them one by one. Given n a
- SMT操作员是做什么的?工作职责?
- Literature research (I): hourly energy consumption prediction of office buildings based on integrated learning and energy consumption pattern classification
猜你喜欢
![[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)](/img/d9/935bad29005e5846dc514c966e3b0e.png)
[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)

《SQL优化核心思想》

Redis memory elimination mechanism

yolov5 提速多GPU训练显存低的问题

Recherche documentaire (3): examen des modèles de prévision de la consommation d'énergie des bâtiments fondés sur les données

Literature research (IV): Hourly building power consumption prediction based on case-based reasoning, Ann and PCA

10.4.1 data console

EBS R12.2.0升级到R12.2.6

POSTMAN测试出现SSL无响应

安装PSU的时候/usr/bin/ld:warning: -z lazyload ignore
随机推荐
Frequently asked questions about redis
Efficacy of kiwi fruit enzyme_ Old bear passing by_ Sina blog
Object array de duplication
Transformation of communication protocol between Siemens S7-200PLC and Danfoss inverter_ Old bear passing by_ Sina blog
Multi-Instance Redo Apply
Smt贴片加工出现元件立碑的解决方法
dhcp复习
Multi-Instance Redo Apply
ffmpeg 版本切换
[reprint]rslogix 5000 instance tutorial
DHCP review
2021-04-28
What is micro service
ASA如何配置端口映射及PAT
farsync 简易测试
Sentinel of redis
mysql 主从复制
DNS复习
Mysql5.7.31自定义安装详细说明
postman如何测试需要登录的接口