package cn.itcast.response;

 import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ResponseDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String path = this.getServletContext().getRealPath("/download/日本妞.jpg");
String filename = path.substring(path.lastIndexOf("\\") + 1); //如果下载文件是中文文件,则文件名需要经过URL编码
response.setHeader("content-disposition", "attachment;filename="
+ URLEncoder.encode(filename,"UTF-8")); InputStream in = null;
OutputStream out = null; in = new FileInputStream(path);
int len = 0;
byte buffer[] = new byte[1024]; out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
} in.close();
out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }

response实现文件下载的更多相关文章

  1. response与文件下载

    参考博客: http://www.cnblogs.com/lcpholdon/p/4380980.html http://www.cnblogs.com/mingforyou/p/3281945.ht ...

  2. Http响应response(文件下载、验证码)

    Http响应response response:响应 作用: 往浏览器写东西 组成部分: 响应行 响应头 响应体 操作响应行 格式: 协议/版本 状态码 状态码说明 状态码: 1xx:已发送请求 2x ...

  3. 使用response实现文件下载功能

    response.setContentType("text/html;charset=utf-8");  response.setCharacterEncoding("u ...

  4. 使用response实现文件下载注意点

    创建web工程,使用response实现文件的下载. 在webRoot下创建download文件,里面包含要下载的文件,现在把源码贴上来,然后再说我遇到的问题 public class DownLoa ...

  5. java web response提供文件下载功能

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  6. response 输出中文数据 文件下载

    使用OutputStream或者PrintWriter向客户端浏览器输出中文数据 package com.xc.response; import java.io.IOException; import ...

  7. spring4 文件下载功能

    需要准备的工具和框架 Spring 4.2.0.RELEASE Bootstrap v3.3.2 Maven 3 JDK 1.7 Tomcat 8.0.21 Eclipse JUNO Service ...

  8. JavaWeb基础:Servlet Response

    ServletResponse简介 ServletResponse代表浏览器输出,它提供所有HttpResponse的设置接口,可以设置HttpResponse的响应状态,响应头和响应内容. 生命周期 ...

  9. ASP.NET 实现PDF文件下载

    本文介绍了一种在ASP.NET中下载文件的方法. 方法一:可能是最简单的.最短的方式: Response.ContentType = "application/pdf"; Resp ...

随机推荐

  1. IOS的Safari浏览器中,点击事件失效的原理及解决办法

    这里做了事件委托,简单区分一下[目标元素]和[代理元素],为后续论述理解做铺垫. [目标元素]:实际希望点击的元素,可以是任意标签. [代理元素]:代替[目标元素]触发点击事件的元素,有可能是目标元素 ...

  2. Javascript-jQuery【1】-用promise()实现html()回调函数

    $('#divId').html(someText).promise().done(function(){ //your callback logic / code here });

  3. Code Rush插件

    code rush 是微软推出的一款VS2008上的插件.他有强大的文件和代码导航功能,易于访问的重构和代码创建功能.一组编辑器.选择.剪贴板工具等. 教程链接 http://www.devexpre ...

  4. Money类型转化为String去除小数点后0解决方法

    Money类型转化为String去除小数点后0从数据库提取Money类型后,字符串如:1212.0000 如何使其成为1212             注:去掉了小数点 如果是:1212.0100 使 ...

  5. iOS之04-方法的声明和实现

    本次重点学习和理解OC对象方法的声明和定义 代码: /* 计算器类 方法: 1> 返回 π 2> 计算某个整数的平方 3> 计算两个整数的和 */ #import <Found ...

  6. 【POJ3237】Tree 树链剖分+线段树

    [POJ3237]Tree Description You are given a tree with N nodes. The tree's nodes are numbered 1 through ...

  7. UIView 的属性opaque详解

    该属性用法决定与该消息的接受者是否让其视图透明. 该属性的用处在于:给绘图系统提供一个性能优化开关. 当该值设计为YES,那么绘图系统会把它当作不透明看待,那么在执行绘图的时候会优化绘图一些操作,并且 ...

  8. TYVJ P3522 &&洛谷 P1135 奇怪的电梯 Label:bfs

    题目描述 呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯.大楼的每一层楼都可以停电梯,而且第i层楼(1<=i<=N)上有一个数字Ki(0<=Ki<=N).电梯只有四个按钮:开 ...

  9. HDU - Pseudoforest

    Description In graph theory, a pseudoforest is an undirected graph in which every connected componen ...

  10. POJ 2402 Palindrome Numbers

    题目链接 水题,LA我居然没找到在那里. #include <cstdio> #include <cstring> #include <string> #inclu ...