servlet实现文件下载
public class DownloadServlet extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
try
{
String downFilename=request.getParameter("curfile");
String filepath=request.getParameter("path");
response.setContentType("text/plain");
response.setHeader("Location",downFilename);
response.setHeader("Content-Disposition", "attachment; filename=" + downFilename);
OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(filepath+downFilename);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
outputStream.close();
}catch(FileNotFoundException e1)
{
System.out.println("没有找到您要的文件");
}
catch(Exception e)
{
System.out.println("系统错误,请及时与管理员联系");
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
}
web.xml配置:
<servlet>
<servlet-name>download</servlet-name>
<servlet-class>com.dichain.common.tools.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/Download</url-pattern>
</servlet-mapping>
jsp中调用servlet弹出一个下载对话框:
window.open ('Download?curfile='+curfile+'&path='+path,'newwindow','height=30,width=40,top='+yy+',left='+xx+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
servlet实现文件下载的更多相关文章
- Servlet处理文件下载的编码问题,乱码。
Servlet处理文件下载的编码问题,乱码. //处理文件名乱码问题 // 获得请求头中的User-Agent String agent = request.getHeader("User- ...
- servlet实现文件下载所需步骤及说明
servlet实现文件下载所需步骤及说明 CreateTime--2017年9月1日15:46:22 Author:Marydon 参考链接:http://blog.sina.com.cn/s/b ...
- Servlet 之文件下载
Servlet 之文件下载 import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; impor ...
- 利用Tomcat内置的servlet实现文件下载功能
起因 最近博客所在的VPS挂了又要重装系统,又要重装各种软件. 以前我也经常更换VPS,每次更换都是各种坑爹事情..比如要下载java.下载tomcat.下载mysql..........以前每次我都 ...
- JQuery调用Servlet实现文件下载
jsp页面上的txt附件,点击后浏览器默认直接打开,结果是乱码. 因为用户上传的txt文件可能是ANSI.Unicode.UTF-8编码的任意一种,上传时后台获取文件内容重写一遍保证浏览器打开正常太过 ...
- Jetty + Servlet 实现文件下载
Jetty非常适合做嵌入式web开发,正如Jetty的口号"Don’t deploy your application in Jetty, deploy Jetty in your appl ...
- jsp使用servlet实现文件下载
1.在index.jsp写入如下代码 <a href="demo2">下载</a> 2.在src中创建ServletDemo2类 public class ...
- Servlet (三) 文件下载(只支持英文文件名)
package cn.sasa.serv; import java.io.FileInputStream; import java.io.IOException; import java.io.Inp ...
- 通过Servlet设置文件下载
文件下载 1.获取要下载的文件的绝对路径 但是使用getServletContext().getRealPath()方法在不同的服务器上所获得的实现是不一样的 因为项目被打包入.war文件以后就失去了 ...
随机推荐
- loadrunner-增加检查点(web_reg_find)
接口性能测试地址: http://192.168.x.x:x/tionWeb/Ajax_GetStock.aspx?stockcode=600571 性能测试脚本: Action() { lr_sta ...
- python 实现斐波那契数列
def fib(n): a,b=0,1 while a<n: print(a,end=" ") a,b=b,a+b print() fib(2000) 输出: 0 1 1 2 ...
- 【转载】错误:ORA-28002: the password will expire within 7 days 解决方法
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:xwdreamer 原文地址: 错误:ORA-28002: the ...
- C++ Template之技巧性基础知识
1.对于T是自定义类型的,如果存在子类型则需要在模版内部加上typename 示例代码: template<typename T> class Myclass { typename T:: ...
- Segment Tree 分类: ACM TYPE 2014-08-29 13:04 97人阅读 评论(0) 收藏
#include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...
- The 15th Zhejiang University Programming Contest
a ZOJ 3860 求和大家不一样的那个数,签到,map水之 #include<cstdio> #include<map> using namespace std; map ...
- NPC AI驱动最基本过程
NPC AI驱动最基本过程 NPCmgr中比较重要的是加载NPC和一个NPCAI的一个指针 他利用map那个线程的定时到底做了啥呢 void NPCmgr::npcAITimer() { time_t ...
- destoon使用中的一些心得
//**************************index首页相关参数**************************************// //全局变量 {if $seo_titl ...
- try-catch语句讲解
try-catch 语句由一个 try 块后跟一个或多个 catch 子句构成,这些子句指定不同的异常处理程序. 引发异常时,公共语言运行时 (CLR) 会查找处理此异常的 catch 语句. 如果当 ...
- Confluence Wiki Markup & Markdown
Markup : 默认有支持 Markdown : 需先安装插件,插件下载地址: Confluence markdown : https://marketplace.atlassian.com/plu ...