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

Keil compilation run error, missing error: # 5: # includecore_ cm3.h_ Old bear passing by_ Sina blog

Redis之跳跃表

About Simple Data Visualization

Establishment of multiple background blocks in botu software_ Old bear passing by_ Sina blog

Recommended system design

How to configure SQL Server 2008 Manager_ Old bear passing by_ Sina blog

文献调研(二):基于短期能源预测的建筑节能性能定量评估

什么是微服务

About Simple Data Visualization
![[advanced ROS] Lecture 1 Introduction to common APIs](/img/25/85e8c55605f5cc999a8e85f0a05f93.jpg)
[advanced ROS] Lecture 1 Introduction to common APIs
随机推荐
10.2.3、Kylin_kylin的使用,维度必选
Read CSV file data in tensorflow
10.4.1 données intermédiaires
oracle RAC 集群无法启动
redux工作流程+小例子的完整代码
如何绕过SSL验证
How postman tests interfaces that require login
Redis之哨兵
Object array de duplication
ASA如何配置端口映射及PAT
Number array de duplication in JS
[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)
Topic36——53. 最大子数组和
Literature research (IV): Hourly building power consumption prediction based on case-based reasoning, Ann and PCA
Introduction to anchor free decision
文獻調研(三):數據驅動的建築能耗預測模型綜述
ffmpeg 版本切换
About Simple Data Visualization
darkent2ncnn出错
Literature research (III): overview of data-driven building energy consumption prediction models