六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 48|回复: 0

下载链接直接打开

[复制链接]

升级  48%

32

主题

32

主题

32

主题

秀才

Rank: 2

积分
122
 楼主| 发表于 2013-1-15 02:46:46 | 显示全部楼层 |阅读模式
关键字: 点击pdf/word等链接时时, 直接打开而不是下载的方法
查看复制到剪切板打印

   1. <%@ page session="false" pageEncoding="UTF-8"%>   
   2. <%@page import="java.io.*"%>   
   3. <%   
   4.   response.setContentType("application/pdf");   
   5.   
   6.   out.clearBuffer(); // 如果使用JSP,需要加上这一句     
   7.   OutputStream os = response.getOutputStream(); // 页面输出流,jsp/servlet都可以     
   8.   response.addHeader("Content-Disposition", new String(("filename=pattern.pdf")   
   9.       .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名     
  10.   File f = new File(application.getRealPath(".")+"/pattern.pdf"); // 你的文件     
  11.   InputStream is = new FileInputStream(f); // 文件输入流     
  12.   byte[] bs = new byte[1024]; // 读取缓冲区     
  13.   int len;   
  14.   while ((len = is.read(bs)) != -1) { // 循环读取     
  15.     os.write(bs, 0, len); // 写入到输出流     
  16.   }   
  17.   is.close(); // 关闭     
  18.   os.close(); // 关闭   
  19. %>  

Java代码

   1. <%@ page session="false" pageEncoding="UTF-8"%>  
   2. <%@page import="java.io.*"%>  
   3. <%  
   4.   response.setContentType("application/pdf");  
   5.   
   6.   out.clearBuffer(); // 如果使用JSP,需要加上这一句   
   7.   OutputStream os = response.getOutputStream(); // 页面输出流,jsp/servlet都可以   
   8.   response.addHeader("Content-Disposition", new String(("filename=pattern.pdf")  
   9.       .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名   
  10.   File f = new File(application.getRealPath(".")+"/pattern.pdf"); // 你的文件   
  11.   InputStream is = new FileInputStream(f); // 文件输入流   
  12.   byte[] bs = new byte[1024]; // 读取缓冲区   
  13.   int len;  
  14.   while ((len = is.read(bs)) != -1) { // 循环读取   
  15.     os.write(bs, 0, len); // 写入到输出流   
  16.   }  
  17.   is.close(); // 关闭   
  18.   os.close(); // 关闭  
  19. %>  

<%@ page session="false" pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<%
  response.setContentType("application/pdf");

  out.clearBuffer(); // 如果使用JSP,需要加上这一句  
  OutputStream os = response.getOutputStream(); // 页面输出流,jsp/servlet都可以  
  response.addHeader("Content-Disposition", new String(("filename=pattern.pdf")
      .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名  
  File f = new File(application.getRealPath(".")+"/pattern.pdf"); // 你的文件  
  InputStream is = new FileInputStream(f); // 文件输入流  
  byte[] bs = new byte[1024]; // 读取缓冲区  
  int len;
  while ((len = is.read(bs)) != -1) { // 循环读取  
    os.write(bs, 0, len); // 写入到输出流  
  }
  is.close(); // 关闭  
  os.close(); // 关闭
%>



请注意这一句
查看复制到剪切板打印

   1. response.addHeader("Content-Disposition", new String(("filename=pattern.pdf")   
   2.       .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名   

Java代码

   1. response.addHeader("Content-Disposition", new String(("filename=pattern.pdf")  
   2.       .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名   

response.addHeader("Content-Disposition", new String(("filename=pattern.pdf")
      .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名  


运行效果


如果要下载的话,就改成
查看复制到剪切板打印

   1. response.addHeader("Content-Disposition", new String(("attachment; filename=pattern.pdf")   
   2.       .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名   

Java代码

   1. response.addHeader("Content-Disposition", new String(("attachment; filename=pattern.pdf")  
   2.       .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名   

response.addHeader("Content-Disposition", new String(("attachment; filename=pattern.pdf")
      .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名  



运行效果




这个东西在http协议里面有规定。

顺便说一句,filename是你下载或者另存为时的文件名,必须用iso-8859-1的编码才可以。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表