Java入门级文件下载_学习笔记
2024-09-02 16:12:17
文件下载和上一篇文件上传很像,就不多说了,传一个我写的一个下载歌曲的代码:
下面是Servlet代码:
public class DownLoadServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext= request.getServletContext();
String filename="风吹麦浪.mp3";
String path=servletContext.getRealPath("/WEB-INF/"+filename);
File file =new File(path);
String type=servletContext.getMimeType(path);
InputStream in = new FileInputStream(file);
response.setContentType(type);
filename =new String(filename.getBytes("gbk"),"iso8859-1");
response.setHeader("Content-Disposition", "attachment;filename"+filename);
PrintWriter out=response.getOutputStream();
IOUtils.copy(in, out);
in.close();
}
}
下面是jsp代码:
<body>
<a href="${pageContext.request.contextPath}/DownLoadServlet">下载</a>
</body>
注意要导入一个io的jar包:
最新文章
- Flexible 弹性盒子模型之CSS flex-basis 属性
- 【转载】Shell判断字符串包含关系的几种方法
- .gitignore 使用中注意的问题
- spring mvc+ELK从头开始搭建日志平台
- Oracle 游标使用全解(转)
- 关于Thread.getContextClassLoader的使用场景问题
- 【转】PHP date(";Y-m-d H:i:s";);获取当前时间 差8小时解决办法
- 锋利的JQuery(二)
- 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- 第一次编辑JAVA
- DICOM开源库
- Apache服务器学习笔记
- C语言中无符号数和有符号数之间的运算
- Android查缺补漏(View篇)--自定义 View 的基本流程
- 小老虎的微信公众号:iITObserve
- CentOS 7 yum 安装php5.6
- apache 80端口占用问题
- kafka可视化工具kafka tools
- SyncthingTray -- Syncthing wrapper for Windows
- C# GDI+技术
热门文章
- GaussDB数据dump实现完全同步
- MySQL--mysql中You can’t specify target table for update in FROM clause错误解决方法
- 用Emoji和照片挑战大众点评,YOBO玩转新点评方式能引领潮流吗?
- Half of UK 10-year-olds own a smartphone
- python编程:从入门到实践----第五章:if语句>;练习
- Linux环境系列 之【配置虚拟机】
- Akka Typed系列:协议&;行为
- SVN的import和export的使用
- 2019CSP-J游记
- look and say 外观数列的python实现