文件下载和上一篇文件上传很像,就不多说了,传一个我写的一个下载歌曲的代码:

下面是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包:

最新文章

  1. Flexible 弹性盒子模型之CSS flex-basis 属性
  2. 【转载】Shell判断字符串包含关系的几种方法
  3. .gitignore 使用中注意的问题
  4. spring mvc+ELK从头开始搭建日志平台
  5. Oracle 游标使用全解(转)
  6. 关于Thread.getContextClassLoader的使用场景问题
  7. 【转】PHP date(&quot;Y-m-d H:i:s&quot;);获取当前时间 差8小时解决办法
  8. 锋利的JQuery(二)
  9. 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
  10. 第一次编辑JAVA
  11. DICOM开源库
  12. Apache服务器学习笔记
  13. C语言中无符号数和有符号数之间的运算
  14. Android查缺补漏(View篇)--自定义 View 的基本流程
  15. 小老虎的微信公众号:iITObserve
  16. CentOS 7 yum 安装php5.6
  17. apache 80端口占用问题
  18. kafka可视化工具kafka tools
  19. SyncthingTray -- Syncthing wrapper for Windows
  20. C# GDI+技术

热门文章

  1. GaussDB数据dump实现完全同步
  2. MySQL--mysql中You can’t specify target table for update in FROM clause错误解决方法
  3. 用Emoji和照片挑战大众点评,YOBO玩转新点评方式能引领潮流吗?
  4. Half of UK 10-year-olds own a smartphone
  5. python编程:从入门到实践----第五章:if语句&gt;练习
  6. Linux环境系列 之【配置虚拟机】
  7. Akka Typed系列:协议&amp;行为
  8. SVN的import和export的使用
  9. 2019CSP-J游记
  10. look and say 外观数列的python实现