123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ page import="java.io.FileInputStream"%>
- <%@ page import="java.io.File"%>
- <%@ page import="java.io.OutputStream"%>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <%
- //若使用字节流进行文件下载,应该首先清空一下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){
- }
- }
- %>
- <script></script>
- <head>
-
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- </head>
-
- <body>
- This is my JSP page. <br>
- </body>
- </html>
|