使用Servlet实现下载文件的功能
在前台有一个下载链接,比如
<a href="DownLoadServlet">下载</a> <br/>
使用Servlet实现下载:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DownLoadServlet extends HttpServlet { public DownLoadServlet() {
super();
} public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//处理请求
//读取要下载的文件
File f = new File("E:/好久不见.mp3");
if(f.exists()){
FileInputStream fis = new FileInputStream(f);
String filename=URLEncoder.encode(f.getName(),"utf-8"); //解决中文文件名下载后乱码的问题
byte[] b = new byte[fis.available()];
fis.read(b);
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition","attachment; filename="+filename+"");
//获取响应报文输出流对象
ServletOutputStream out =response.getOutputStream();
//输出
out.write(b);
out.flush();
out.close();
} } /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
} }
配置文件注意路径。。。
本文来源于 :http://www.cnblogs.com/android-html5/archive/2012/03/16/2534082.html
使用Servlet实现下载文件的功能的更多相关文章
- [转]python3之paramiko模块(基于ssh连接进行远程登录服务器执行命令和上传下载文件的功能)
转自:https://www.cnblogs.com/zhangxinqi/p/8372774.html 阅读目录 1.paramiko模块介绍 2.paramiko的使用方法 回到顶部 1.para ...
- Java Servlet实现下载文件
一.配置servlet 在WebContent(以前的eclipse版本是WebRoot)文件夹下,有一个web.xml 修改web.xml ,加入以下代码 <servlet> <s ...
- servlet 实现下载文件
servlet: public class UpAndDownServlet extends HttpServlet { public void doPost(HttpServletRequest r ...
- Servlet:浏览器下载文件时文件名为乱码问题
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcep ...
- 【Servlet】java web 文件下载功能实现
需求:实现一个具有文件下载功能的网页,主要下载压缩包和图片 两种实现方法: 一:通过超链接实现下载 在HTML网页中,通过超链接链接到要下载的文件的地址 <!DOCTYPE html> & ...
- Python扩展模块——selenium的使用(定位、下载文件等)
想全面的使用selenium可以下载<selenium 2自动化测试实战-基于Python语言>PDF的电子书看看 我使用到了简单的浏览器操作,下载文件等功能... 推荐使用firefox ...
- js发送post请求,实现下载文件
由于业务需求要下载文件的功能: <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- jQuery --- 利用a标签的download属性下载文件!
最近遇到一个项目,需要有点击下载文件的功能. 由于文件格式是多种的,对于 rar / zip / rtf / doc / xlsx / jpg等. 点击下载有的是直接跳转到后进行下载,但有的是打开进行 ...
- c# ftp 上传文件 与 下载文件
接着上一篇说. 上一篇说了根据配置文件获取路径,并判断路径在服务器中是否存在.如果不存在则在服务器中建立一个. 然后就是往路径下面传输文件了.. 代码: //连接ftp private void Co ...
随机推荐
- 【转载国外好文】代工开发一个iOS应用没有那么容易
导读:这是来自新加坡的 iOS 开发者 Kent Nguyen 发表在1月底的一篇博文.这篇吐槽文在 iOS 开发圈子里流传甚广,从原文150多个评论就可见一斑,现翻译如下. 让我们开门见山吧:做一个 ...
- C#读写app.config中的数据
C#读写app.config中的数据 读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Con ...
- HTML标签----图文详解(二)
HTML标签超详细的图文演示再来一波~~~ 如果还没有看过昨天的福利的,那可要抓紧喽,传送门:HTML标签----图文详解 本文主要内容 列表标签 表格标签 框架标签及内嵌框架<iframe&g ...
- qau-国庆七天乐——B
B - Bull Math Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Sub ...
- uva10001 Garden of Eden
Cellular automata are mathematical idealizations of physical systems in which both space and time ar ...
- 比较全的JavaScript倒计时脚本[xyytit]
需要做一个功能,给特定的活动或者商品添加一个倒计时提示,在网上找了好些方法,总结了比较好的一些方法,以备后用: 1. 比较长时间的倒计时(如:距离2014年还有0年, 0月, 30天, 9小时, 41 ...
- HOJ 2713 Matrix1
Matrix1 My Tags (Edit) Source : Classical Problem Time limit : 5 sec Memory limit : 64 M Sub ...
- memcached的图形界面监控
前提是已经安装了php和memcached 图形界面的监控是通过memcache.php来实现的, 1.把该php程序拷贝到apache的web根目录 [root@cacti srv]# ...
- BZOJ 3295 【Cqoi2011】 动态逆序对
Description 对于序列\(A\),它的逆序对数定义为满足\(i<j\),且\(A_i>A_j\)的数对\((i,j)\)的个数.给\(1\)到\(n\)的一个排列,按照某种顺序依 ...
- android studio 使用问题 解决方法
1. Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.ap ...