当前位置:网站首页>Servlet response下载文件
Servlet response下载文件
2022-06-25 22:13:00 【大魔王的日常Log】
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;
/**
* 文件下载
*/
@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();
//设置mine
String mt = s.getMimeType(filename);
response.setContentType(mt);
//设置下载头信息
response.setHeader("content-disposition", "attchment;filename="+filename);
//对考流
//获取输入流
InputStream is = s.getResourceAsStream("/download/"+filename);
//获取输出流
ServletOutputStream os = response.getOutputStream();
/* 第一种
int len=-1;
byte[] b=new byte[1024];
while ((len=is.read(b))!=-1) {
os.write(b,0,len);
}*/
// 第二种:利用工具类的方法commons-io-1.4.rar
IOUtils.copy(is, os);
//关流
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编译运行错误,缺少error:#5:#includecore_cm3.h_过路老熊_新浪博客
- Virtual and pure virtual destructors and their implementation in c++
- 利用swiper实现轮播图
- Redis之哨兵
- How to configure SQL Server 2008 Manager_ Old bear passing by_ Sina blog
- Record some CF questions
- Joint simulation of STEP7 and WinCC_ Old bear passing by_ Sina blog
- huibian
- SMT行业AOI,X-RAY,ICT分别是什么?作用是?
- 给定参数n,从1到n会有n个整数1,2,3,...,n,这n个数组共有n!种排列,按照大小顺序升序排列出所有列的情况,并一一标记,给定n和k,返回第k个值
猜你喜欢

10.4.1 données intermédiaires

Literature research (II): quantitative evaluation of building energy efficiency performance based on short-term energy prediction

86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)

Redis之内存淘汰机制

手工制作 pl-2303hx 的USB轉TTL電平串口的電路_過路老熊_新浪博客

Hand made pl-2303hx USB to TTL level serial port circuit_ Old bear passing by_ Sina blog

DHCP review

使用npm创建并发布包时遇到的常见问题

About the solution to prompt modulenotfounderror: no module named'pymongo 'when running the scratch project

详细讲解局部变量、全局变量、静态变量三种类型
随机推荐
Redis之哨兵
step7和wincc联合仿真_过路老熊_新浪博客
Alipay payment interface sandbox environment test and integration into an SSM e-commerce project
ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided解决
利用VBScript连接mysql数据库_过路老熊_新浪博客
剑指 Offer 48. 最长不含重复字符的子字符串
利用swiper实现轮播图
贴片机供料器(feeder)飞达的种类,如何工作
懒人教你用猕猴桃一月饱减16斤_过路老熊_新浪博客
SMT贴片加工PCBA板清洗注意事项
Establishment of multiple background blocks in botu software_ Old bear passing by_ Sina blog
迅为RK3568开发板使用RKNN-Toolkit-lite2运行测试程序
On the quantity control mechanism of swoole collaboration creation in production environment
文献调研(三):数据驱动的建筑能耗预测模型综述
Prototype chain test questions in JS --foo and getname
[转载]RSLOGIX 5000实例教程
Joint simulation of STEP7 and WinCC_ Old bear passing by_ Sina blog
Bit Compressor [蓝桥杯题目训练]
推荐系统设计
redis之集群