日期:2014-05-16  浏览次数:20348 次

(转)JSP文件下载
<%--    
直接在JSP页面中进行文件下载的代码(改 Servlet 或者   
JavaBean 的话自己改吧), 支持中文附件名(做了转内码处理). 事实上只要向   
out 输出字节就被认为是附件内容, 不一定非要从文件读取原始数据, 从数据   
库中读取也可以的.   
需传三个参数 newname,name,path   
--%>   
<%@ page contentType=" text/html; charset=utf-8" pageEncoding="utf-8"%>   
<%@ page import="java.io.*,java.util.*,java.text.*"%>   
<%!//If returns true, then should return a 304 (HTTP_NOT_MODIFIED)   
    public static boolean checkFor304(HttpServletRequest req, File file) {   
        //   We'll do some handling for CONDITIONAL GET (and return a 304)   
        //   If the client has set the following headers, do not try for a 304.   
        //     pragma: no-cache   
        //     cache-control: no-cache   
        if ("no-cache".equalsIgnoreCase(req.getHeader("Pragma"))   
                || "no-cache".equalsIgnoreCase(req.getHeader("cache-control"))) {   
            //  Wants specifically a fresh copy    
        } else {   
            //   HTTP 1.1 ETags go first   
            String thisTag = Long.toString(file.lastModified());   
            String eTag = req.getHeader("If-None-Match");   
            if (eTag != null) {   
                if (eTag.equals(thisTag)) {   
                    return true;   
                }   
            }   
            //   Next, try if-modified-since   
            DateFormat rfcDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");   
            Date lastModified = new Date(file.lastModified());   
            try {   
                long ifModifiedSince = req.getDateHeader("If-Modified-Since");   
                // log.info("ifModifiedSince:"+ifModifiedSince);    
                if (ifModifiedSince != -1) {   
                    long lastModifiedTime = lastModified.getTime();   
                    // log.info("lastModifiedTime:" + lastModifiedTime);    
                    if (lastModifiedTime <= ifModifiedSince) {   
                        return true;   
                    }   
                } else {   
                    try {   
                        String s = req.getHeader(" If-Modified-Since ");   
                        if (s != null) {   
                            Date ifModifiedSinceDate = rfcDateFormat.parse(s);   
                            // log.info("ifModifiedSinceDate:" + ifModifiedSinceDate);    
                            if (lastModified.before(ifModifiedSinceDate)) {   
                                return true;   
                            }   
                        }   
                    } catch (ParseException e) {   
                        // log.warn(e.getLocalizedMessage(), e);    
                    }   
                }   
            } catch (IllegalArgumentException e) {   
                //  Illegal date/time header format.   
                //  We fail quietly, and return false.   
                //  FIXME: Should really move to ETags.    
            }   
        }   
        return false;   
    }%>   
<%   
      String newname=request.getParameter("upfile_newname");   
      String name=request.getParameter("upfile_name");   
      String path=request.getParameter("upfile_path");   
    //  String filePath = "c:/文档.doc";   
    //  如果是 WEB APP 下的相对路径文件, 请使用下列代码:    
    if(path.charAt(0)=='/') path=path.substring(1);   
    String filePath = application.getRealPath(path+"/"+newname);   
    boolean isInline = false; //  是否允许直接在浏览器内打开(如果浏览器能够预览此文件内容,   
    //  那么文件将被打开, 否则会提示下载)   
    //  清空缓冲区, 防止页面中的空行, 空格添加到要下载的文件内容中去   
    //  如果不清空的话在调用 response.reset() 的时候 Tomcat 会报错   
    //  java.lang.IllegalStateException: getOutputStream() has already been called for   
    //  this response,    
    out.clear();   
    //  {{{ BEA Weblogic 必读   
    //  修正 Bea Weblogic 出现 "getOutputStream() has already been called for this response"错误的问题   
    //  关于文件下载时采用文件流输出的方式处理:   
    //  加上response.reset(),并且所有的%>后面不要换行,包括最后一个;   
    //  因为Applicat