欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
JSP 文件下載的相對完整代碼(解決中文問(wèn)題, Weblogic 異常)
JSP 文件下載的相對完整代碼(解決中文問(wèn)題, Weblogic 異常)
 
<%--
有些朋友詢(xún)問(wèn)使用 JSP Smart 下載文件的時(shí)候報錯, 這里給出一個(gè)測試過(guò)的不
需要使用 JSP Smart JSP 頁(yè)面中進(jìn)行文件下載的代碼(改 Servlet 或者
JavaBean 的話(huà)自己改吧), 支持中文附件名(做了轉內碼處理). 事實(shí)上只要向
out 輸出字節就被認為是附件內容, 不一定非要從文件讀取原始數據, 從數據
庫中讀取也可以的.
測試結果: Tomcat 5.0, 5.5, Resin 3.0.18 , Weblogic 8.1, 9.2 測試通過(guò), 無(wú)異常產(chǎn)生
--%>
<%@ page contentType="text/html; charset=GBK" pageEncoding="GBK" %>
<%@ 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 filePath = "c:/文檔.doc";
// 如果是 WEB APP 下的相對路徑文件, 請使用下列代碼:
String filePath = application.getRealPath("測試文檔.htm");
boolean isInline = false;// 是否允許直接在瀏覽器內打開(kāi)(如果瀏覽器能夠預覽此文件內容,
// 那么文件將被打開(kāi), 否則會(huì )提示下載)
// 清空緩沖區, 防止頁(yè)面中的空行, 空格添加到要下載的文件內容中去
// 如果不清空的話(huà)在調用 response.reset() 的時(shí)候 Tomcat 會(huì )報錯
// 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"錯誤的問(wèn)題
    // 關(guān)于文件下載時(shí)采用文件流輸出的方式處理:
    // 加上response.reset(),并且所有的%>后面不要換行,包括最后一個(gè);
    // 因為Application Server在處理編譯jsp時(shí)對于%>和<%之間的內容一般是原樣輸出,而且默認是PrintWriter,
    // 而你卻要進(jìn)行流輸出:ServletOutputStream,這樣做相當于試圖在Servlet中使用兩種輸出機制,
    // 就會(huì )發(fā)生:getOutputStream() has already been called for this response的錯誤
    // 詳細請見(jiàn)《More Java Pitfill》一書(shū)的第二部分 Web層Item 33:試圖在Servlet中使用兩種輸出機制 270
    // 而且如果有換行,對于文本文件沒(méi)有什么問(wèn)題,但是對于其它格式,比如AutoCAD、Word、Excel等文件
    //下載下來(lái)的文件中就會(huì )多出一些換行符0x0d和0x0a,這樣可能導致某些格式的文件無(wú)法打開(kāi),有些也可以正常打開(kāi)。
    // 同時(shí)這種方式也能清空緩沖區, 防止頁(yè)面中的空行等輸出到下載內容里去
    response.reset();
    // }}}
try {
java.io.File f = new java.io.File(filePath);
if (f.exists() && f.canRead()) {
// 我們要檢查客戶(hù)端的緩存中是否已經(jīng)有了此文件的最新版本, 這時(shí)候就告訴
// 客戶(hù)端無(wú)需重新下載了, 當然如果不想檢查也沒(méi)有關(guān)系
            if( checkFor304( request, f ) )
            {
                // 客戶(hù)端已經(jīng)有了最新版本, 返回 304
                response.sendError( HttpServletResponse.SC_NOT_MODIFIED );
                return;
            }
// 從服務(wù)器的配置來(lái)讀取文件的 contentType 并設置此contentType, 不推薦設置為
// application/x-download, 因為有時(shí)候我們的客戶(hù)可能會(huì )希望在瀏覽器里直接打開(kāi),
// Excel 報表, 而且 application/x-download 也不是一個(gè)標準的 mime type,
// 似乎 FireFox 就不認識這種格式的 mime type
String mimetype = null;
mimetype = application.getMimeType( filePath );
if( mimetype == null )
{
    mimetype = "application/octet-stream;charset=ISO8859-1";
}
response.setContentType( mimetype );
// IE 的話(huà)就只能用 IE 才認識的頭才能下載 HTML 文件, 否則 IE 必定要打開(kāi)此文件!
String ua = request.getHeader("User-Agent");// 獲取終端類(lèi)型
if(ua == null) ua = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;)";
boolean isIE = ua.toLowerCase().indexOf("msie") != -1;// 是否為 IE
if(isIE && !isInline) {
mimetype = "application/x-msdownload";
}
// 下面我們將設法讓客戶(hù)端保存文件的時(shí)候顯示正確的文件名, 具體就是將文件名
// 轉換為 ISO8859-1 編碼
String downFileName = new String(f.getName().getBytes(), "ISO8859-1");
String inlineType = isInline ? "inline" : "attachment";// 是否內聯(lián)附件
// or using this, but this header might not supported by FireFox
//response.setContentType("application/x-download");
response.setHeader ("Content-Disposition", inlineType + ";filename=\""
+ downFileName + "\"");
response.setContentLength((int) f.length());// 設置下載內容大小
        byte[] buffer = new byte[4096];// 緩沖區
        BufferedOutputStream output = null;
        BufferedInputStream input = null;
        //
        try {
            output = new BufferedOutputStream(response.getOutputStream());
            input = new BufferedInputStream(new FileInputStream(f));
            int n = (-1);
            while ((n = input.read(buffer, 0, 4096)) > -1) {
                output.write(buffer, 0, n);
            }
            response.flushBuffer();
        }
        catch (Exception e) {
        } // 用戶(hù)可能取消了下載
        finally {
            if (input != null) input.close();
            if (output != null) output.close();
        }
}
return;
} catch (Exception ex) {
  //ex.printStackTrace();
}
// 如果下載失敗了就告訴用戶(hù)此文件不存在
response.sendError(404);
%>
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
servlet 和JSP的上傳下載
JSP文件下載的解決方法
JSP動(dòng)態(tài)輸出Excel及中文亂碼的解決
jsp文件下載完整方法
文件下載
JSP面試(經(jīng)典) 轉帖
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久