request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
//1、设置响应头
response.setContentType("application/force-download");
//2、读取文件
String path=getServletContext().getRealPath("/file/"+name);
InputStream in=new FileInputStream(path);
//3、对文件名进行编码
name=URLEncoder.encode(name, "utf-8");
//4、设置响应头
response.setHeader("Content-Disposition","attachment;filename="+name);//如果不写attachment,则会在浏览器中打开
response.setContentLength(in.available());
//5、开始copy文件
OutputStream out=response.getOutputStream();
byte[] b=new byte[1024];
int len=-1;
while((len=in.read(b))!=-1)
{
out.write(b, 0, len);
}
out.close();
in.close(); }

写在Servlet中,可以防止盗链,进行过滤等操作

Java下载Servlet Demo的更多相关文章

  1. java下载文件demo

    java通过http方式下载文件 https://www.cnblogs.com/tiancai/p/7942201.html

  2. Java基础——Servlet(八)文件上传下载

    一.简单的文件上传常见的组件Smartupload , Apache 的 commons FileUploadSmartupload上传的步骤: 1.初始化上传上下文 2.准备上传 3.保存文件 &l ...

  3. 启动servlet报错:The servlets named [DemoServlet] and [main.java.com.wlf.demo.servlet.DemoServlet] are both mapped to the url-pattern [/hello] which is not permitted

    先看具体错误日志: [2019-04-26 09:29:25,484] Artifact demo-servlet:war: Artifact is being deployed, please wa ...

  4. 如何使用Java、Servlet创建二维码

    归功于智能手机,QR码逐渐成为主流,它们正变得越来越有用.从候车亭.产品包装.家装卖场.汽车到很多网站,都在自己的网页集成QR码,让人们快速找到它们.随着智能手机的用户量日益增长,二维码的使用正在呈指 ...

  5. 【转】如何使用Java、Servlet创建二维码

    归功于智能手机,QR码逐渐成为主流,它们正变得越来越有用.从候车亭.产品包装.家装卖场.汽车到很多网站,都在自己的网页集成QR码,让人们快速找到它们.随着智能手机的用户量日益增长,二维码的使用正在呈指 ...

  6. (转)如何使用Java、Servlet创建二维码

    归功于智能手机,QR码逐渐成为主流,它们正变得越来越有用.从候车亭.产品包装.家装卖场.汽车到很多网站,都在自己的网页集成QR码,让人们快速找到它们.随着智能手机的用户量日益增长,二维码的使用正在呈指 ...

  7. Java之Servlet文件下载20190228

    jsp页面: <%@ page language="java" contentType="text/html; charset=utf-8" pageEn ...

  8. java下载文件工具类

    java下载文件工具类 package com.skjd.util; import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  9. java下载安装,环境变量,hello world

    1.Java下载安装 网址:http://java.sun.com/javase/downloads/index.jsp win7 64位选择jdk-8u11-windows-x64.exe. 2.环 ...

随机推荐

  1. Forms & HTML 组件 - laravelcollective/html

    简书链接 :Forms & HTML 组件 - laravelcollective/html 安装 方法一: composer require laravelcollective/html 方 ...

  2. Scut 进阶:Schema 自动检测

    Scut 在启动时有一个自动根据代码中数据类型检查数据库字段的功能,要如何使用呢? 脚本引擎动态加载 ModelAssembly: ScriptEngine.cs - InitScriptRuntim ...

  3. 转:Hprose for php(二)——服务器

    文章来自于:http://blog.csdn.net/half1/article/details/21252879 本文将介绍Hprose for php服务器的更多细节 1.发布服务 Hprose提 ...

  4. Node.js stream 流学习

    由于node.js 创建http 是这样的 http.createServer(function(request,response){}).listen(2000); 里面的request 就是rea ...

  5. UML问题

    1.在创建协作图时需要先确定参与者,而协作图的工具栏里是没有Actor的,这是需要先new Actor,然后把其拖动到工作区:实验过程中发现必须创建在用例视图下,若是创建在逻辑试图下那么根本无法继续操 ...

  6. Steps

    uva846:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  7. 台积电16nm工艺为什么好过三星14nm

    最近,关于iPhone6s A9处理器版本的事情的话题很热,最后都闹到苹果不得不出来解释的地步,先不评判苹果一再强调的整机综合续航差2~3%的准确性,但是三星14nm工艺相比台积电16nm工艺较差已经 ...

  8. Spark SQL Table Join(Python)

    示例   Spark SQL注册“临时表”执行“Join”(Inner Join.Left Outer Join.Right Outer Join.Full Outer Join)   代码   fr ...

  9. Delphi TdxBarManager通过代码生成菜单

    procedure TForm6.btn1Click(Sender: TObject);var  ABar:TdxBar;  ABarItem:TdxBarItem;  ABarSubItem:Tdx ...

  10. 怎么都没人提 google 加密搜索呢? google如何稳定打开

    1. 使用smarthosts提供的hosts文件 https://smarthosts.googlecode.com/svn/trunk/hosts2. 修改浏览器默认搜索引擎为 https://e ...