- <%@ 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(); // 关闭
- %>
<%@ 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(); // 关闭 %>
请注意这一句
- response.addHeader("Content-Disposition", new String(("filename=pattern.pdf")
- .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名
response.addHeader("Content-Disposition", new String(("filename=pattern.pdf") .getBytes("GBK"), "ISO-8859-1")); // 针对中文文件名
如果要下载的话,就改成
- response.addHeader("Content-Disposition", new String(("attachment; filename=pattern.pdf")
- .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的编码才可以。