%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.io.FileInputStream"%> <%@ page import="java.io.File"%> <%@ page import="java.io.OutputStream"%> <% //若使用字节流进行文件下载,应该首先清空一下out对象,默认是字符流 out.clear(); out = pageContext.pushBody(); response.setContentType("application/force-download"); String path=request.getParameter("path").replace("../..","D:/apache-tomcat-6.0.41/webapps/cetdz"); System.out.println(path); String filename=java.net.URLDecoder.decode(request.getParameter("filename"),"UTF-8"); //filename=path.substring(path.lastIndexOf("/")+1); System.out.println(filename); if (request.getHeader("user-agent").toLowerCase().contains("msie")) { // IE filename = java.net.URLEncoder.encode(filename, "UTF-8"); } else { // 非IE filename = new String(filename.getBytes("UTF-8"), "ISO8859-1"); } //filename=new String(filename.getBytes("UTF-8"),"ISO8859-1"); //filename=path.substring(path.lastIndexOf("/")+1); //通过设置头标题来显示文件传送到前端浏览器的文件信息 response.setHeader("Content-disposition","inline;filename="+filename); File file = new File(path); response.setHeader("Content_Length",String.valueOf(file.length())); FileInputStream input = null; OutputStream output = null; try{ input = new FileInputStream(file); output = response.getOutputStream(); byte size[] = new byte[1024]; int length; while((length=input.read(size))!=-1){ output.write(size); } }catch(Exception e){ System.out.print(e.getMessage()); }finally{ try{ input.close(); output.flush(); output.close(); }catch(Exception ex){ } } %>