chrome 失敗-網絡異常,但是其他瀏覽器正常下載

震驚!!! 新鮮出爐的文件下載功能,在chrome 中慘遭滑鐵盧
本文只針對 在chrome中滑鐵盧 但在其他瀏覽器上攻無不克 
並且新鮮出爐的javaWEB文件下載功能

出現問題的地方在這裏:

// 錯誤代碼
		 response.setContentType("application/xlsx");
// 正確代碼
        response.setContentType("application/xlsx;");
// 一個 ; 號之差 

正確代碼是這樣的:

//           拿到文件對象後的校驗 下載
        if (!file.exists()) {
            ResponseUtils.renderJson(response, JSON.toJSONString(Result.getMsgError("服務器上沒有你需要的模板")));
        }
// 			開始設置響應頭,準備傳輸文件        
//            設置響應內容大小
        response.setHeader("Content-Length", String.valueOf(file.length()));
//             設置響應類型, 此處是xlsx
        response.setContentType("application/xlsx;");
//            設置 Content-Disposition  爲 attachment 如此瀏覽器會激活文件下載框
//             filename 則是 下載的文件的默認名稱
        response.addHeader("Content-Disposition",
                "attachment; filename=" + UPLOAD_TEMPLATE_FILE_NAME);

        byte[] buffer = new byte[1024];
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            OutputStream os = response.getOutputStream();
            int i  ;
            while ((i=bis.read(buffer)) != -1) {
                os.write(buffer, 0, i);
            }
            
         } catch (IOException e) {
            log.warn(" upload file is error .the error msg is :[" + e.getMessage() + "]");
            result = Result.getDefaultError();
            e.printStackTrace();
         } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                }
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章