该文为堕落的天使不再来原创。欢迎转载。
    在尽心web开发时,可能遇到以下几种需求:(disposition配置)

  • 希望某类或者某已知MIME 类型的文件(比如:*.gif;*.txt;*.htm)能够在访问时弹出“文件下载”对话框。
  • 希望客户端下载时以指定文件名显示。
  • 希望某文件直接在浏览器上显示而不是弹出文件下载对话框。

对于上面的需求,使用Content-Disposition属性就可以解决。下面是代码示例:

 response.setHeader("Content-disposition", "attachment;filename=" + fileName)。
  //Content-disposition为属性名。
 //attachment表示以附件方式下载。
如果要在页面中打开,则改为inline。
 //filename如果为中文,则会出现乱码。解决办法有两种: 
//1、使用fileName = new String(fileName.getBytes(), "ISO8859-1")语句
/* 验证content-disposition */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletContext().getRealPath("/picture/桌面壁纸.jpg");
String fileName = path.substring(path.indexOf("\\") + 1);
//1、使用fileName = new String(fileName.getBytes(), "ISO8859-1")语句
fileName = new String(fileName.getBytes(),"ISO8859-1");
// response开始发送响应头
response.setHeader("content-disposition", "attachment;filename="
+ fileName); // 基本的流操作
OutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(path);// 绝对路径
out = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
} finally {
if (in != null) {
try {
// 关闭流
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (out != null) {
try {
// 关闭流
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
}
//2、使用 URLEncoder.encode(fileName,"UTF-8" 语句
    public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletContext().getRealPath(
"/picture/桌面壁纸.jpg");
String fileName = path.substring(path.lastIndexOf("\\") + 1);
// System.out.println(fileName);//桌面壁纸.jpg
/*response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");*///这样是错误的
// 发送响应头,如果文件是中文名,这要经过url编码
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode(fileName,"UTF-8")); // 下载图片
OutputStream out = null;
FileInputStream in = null;
try {
in = new FileInputStream(path);// 绝对路径
out = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
} finally {
if(in != null){
try{
// 关闭流
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(out != null){
try{
// 关闭流
out.close();
}catch(Exception e){
e.printStackTrace();
}
} } } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

最新文章

  1. 嵌入式开发中常见3个的C语言技巧
  2. advstringgrid笔记
  3. 原生JS添加节点方法与jQuery添加节点方法的比较及总结
  4. windows下,用绝对路径向html文件中插入图片
  5. linux下的文件权限
  6. 201521123042 《Java程序设计》第4周学习总结
  7. spring框架应用系列一:annotation-config自动装配
  8. Devops step by step
  9. kali权限提升之本地提权
  10. 2017-11-8—自动控制原理在软硬件方面上的应用和体现
  11. 第五章 JS典型特效
  12. WCF返回表datatable时的解决
  13. P1541 乌龟棋 线性dp
  14. 梦殇 chapter four
  15. Spring Cloud 子项目介绍
  16. Mysql中Left Join 与Right Join 与 Inner Join 与 Full Join的区别
  17. Node.js之Express一
  18. Xwork概况 XWork是一个标准的Command模式实现,并且完全从web层脱离出来。Xwork提供了很多核心功能:前端拦截机(interceptor),运行时表单属性验证,类型转换,强大的表达式语言(OGNL – the Object Graph NavigationLanguage),IoC(Inversion of Control反转控制)容器等。 ----------------
  19. 使用 xhprof 进行 php 的性能分析
  20. phpStudy apache无法启动 apache启动后又停止

热门文章

  1. 利用jQuery和CSS实现环形进度条
  2. js之路
  3. Linux CentOs7 下安装 redis
  4. uart启示1_task的写法
  5. WebLogic 12c控制台上传获取webshell
  6. 未能加载文件或程序集“AspNetPager”或它的某一个依赖项。参数错误(转)
  7. uva 10820
  8. JS的字符串处理
  9. 获取应用程序信息.h
  10. HDU 5936 Difference