JavaWeb下載文件response


以下代碼在 chrome、firefox,安卓自帶手機瀏覽器上測試通過,但未經過完全測試,先記錄下

    public static void downLoadFile(HttpServletRequest request,HttpServletResponse response,String fullPath,String fileName) throws IOException {
        OutputStream outp = response.getOutputStream();
        File file = new File(fullPath);
        if (file.exists()) {
            response.setContentType("APPLICATION/OCTET-STREAM");
       //response.setContentType("application/octet-stream; charset=utf-8"); String filedisplay
= fileName; String agent = (String)request.getHeader("USER-AGENT"); if(agent != null && ( agent.indexOf("MSIE") != -1 || agent.indexOf("Trident") != -1 || agent.indexOf("Mobile") != -1 )) { //移動瀏覽器 或 ie Trident是標識是ie瀏覽器 特別處理ie11 的問題 filedisplay=URLEncoder.encode(filedisplay,"utf-8"); System.out.println("下載文件,移動設備瀏覽器 或 ie[" + filedisplay + "]"); response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay); } else {
         //其他瀏覽器 String enableFileName
= "=?UTF-8?B?" + (new String(Base64.getBase64(filedisplay))) + "?="; System.out.println("下載文件,其他瀏覽器[" + enableFileName + "]"); response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName); } FileInputStream in = null; try { outp = response.getOutputStream(); in = new FileInputStream(fullPath); byte[] b = new byte[1024]; int i = 0; while ((i = in.read(b)) > 0) { outp.write(b, 0, i); } outp.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { in.close(); in = null; } if (outp != null) { outp.close(); outp = null; response.flushBuffer(); } } } else { outp.write("文件不存在!".getBytes("utf-8"));
       outp.close(); } }

 

    /**
     * 獲取下載文件的content-type類型。
     * 
     * @param extName  文件后綴
     * @return
     */
    private String getContextType(String extName, boolean isRead) {
        String contentType = "application/octet-stream";
        if ("jpg".equalsIgnoreCase(extName) || "jpeg".equalsIgnoreCase(extName)) {
            contentType = "image/jpeg";
        } else if ("png".equalsIgnoreCase(extName)) {
            contentType = "image/png";
        } else if ("gif".equalsIgnoreCase(extName)) {
            contentType = "image/gif";
        } else if ("doc".equalsIgnoreCase(extName) || "docx".equalsIgnoreCase(extName)) {
            contentType = "application/msword";
        } else if ("xls".equalsIgnoreCase(extName) || "xlsx".equalsIgnoreCase(extName)) {
            contentType = "application/vnd.ms-excel";
        } else if ("ppt".equalsIgnoreCase(extName) || "pptx".equalsIgnoreCase(extName)) {
            contentType = "application/ms-powerpoint";
        } else if ("rtf".equalsIgnoreCase(extName)) {
            contentType = "application/rtf";
        } else if ("htm".equalsIgnoreCase(extName) || "html".equalsIgnoreCase(extName)) {
            contentType = "text/html";
        } else if ("swf".equalsIgnoreCase(extName)) {
            contentType = "application/x-shockwave-flash";
        } else if ("bmp".equalsIgnoreCase(extName)) {
            contentType = "image/bmp";
        } else if ("mp4".equalsIgnoreCase(extName)) {
            contentType = "video/mp4";
        } else if ("wmv".equalsIgnoreCase(extName)) {
            contentType = "video/x-ms-wmv";
        } else if ("wm".equalsIgnoreCase(extName)) {
            contentType = "video/x-ms-wm";
        } else if ("rv".equalsIgnoreCase(extName)) {
            contentType = "video/vnd.rn-realvideo";
        } else if ("mp3".equalsIgnoreCase(extName)) {
            contentType = "audio/mp3";
        } else if ("wma".equalsIgnoreCase(extName)) {
            contentType = "audio/x-ms-wma";
        } else if ("wav".equalsIgnoreCase(extName)) {
            contentType = "audio/wav";
        }
        if ("pdf".equalsIgnoreCase(extName) && isRead)// txt不下載文件,讀取文件內容
        {
            contentType = "application/pdf";
        }
        if (("sql".equalsIgnoreCase(extName) || "txt".equalsIgnoreCase(extName)) && isRead)// pdf不下載文件,讀取文件內容
        {
            contentType = "text/plain";
        }
        return contentType;
    }

 

其他參考,但未經驗證

http 下載文件時,中文文件名在firefox下亂碼的問題,一般在http header中是這樣操作的:
"Content-Disposition","attachment;filename=文件名.xx" 其實,按照 rfc231 , Content-Disposition 應該按照如下格式設置: "Content-Disposition","attachment;filename*=utf-8'zh_cn'文件名.xx" 只要嚴格按照標准設置以后,自然在各種瀏覽器下都會正常運行了.

 

String userAgent = request.getHeader("User-Agent");
    String rtn = "";
    try {
        String new_filename = URLEncoder.encode(fileName, "UTF8");
        // 如果沒有UA,則默認使用IE的方式進行編碼,因為畢竟IE還是占多數的
        rtn = "filename=\"" + new_filename + "\"";
        if (userAgent != null) {
            userAgent = userAgent.toLowerCase();
            // IE瀏覽器,只能采用URLEncoder編碼
            if (userAgent.indexOf("msie") != -1) {
                rtn = "filename=\"" + new_filename + "\"";
            }
            // Opera瀏覽器只能采用filename*
            else if (userAgent.indexOf("opera") != -1) {
                rtn = "filename*=UTF-8''" + new_filename;
            }
            // Safari瀏覽器,只能采用ISO編碼的中文輸出
            else if (userAgent.indexOf("safari") != -1) {
                rtn = "filename=\"" + new String(fileName.getBytes("UTF-8"), "ISO8859-1") + "\"";
            }
            // Chrome瀏覽器,只能采用MimeUtility編碼或ISO編碼的中文輸出
            else if (userAgent.indexOf("applewebkit") != -1) {
                new_filename = MimeUtility.encodeText(fileName, "UTF8", "B");
                rtn = "filename=\"" + new_filename + "\"";
            }
            // FireFox瀏覽器,可以使用MimeUtility或filename*或ISO編碼的中文輸出
            else if (userAgent.indexOf("mozilla") != -1) {
                rtn = "filename*=UTF-8''" + new_filename;
            }
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return rtn;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM