代碼
10余年的尼勒克網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷(xiāo)推廣的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整尼勒克建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“尼勒克網(wǎng)站設(shè)計(jì)”,“尼勒克網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。參數(shù):
1.filePath:文件的絕對(duì)路徑(d:\download\a.xlsx)
2.fileName(a.xlsx)
3.編碼格式(GBK)
4.response、request不介紹了,從控制器傳入的http對(duì)象
代碼片.
//控制器 @RequestMapping(UrlConstants.BLACKLIST_TESTDOWNLOAD) public void downLoad(String filePath, HttpServletResponse response, HttpServletRequest request) throws Exception { boolean is = myDownLoad("D:\\a.xlsx","a.xlsx","GBK",response,request); if(is) System.out.println("成功"); else System.out.println("失敗"); } //下載方法 public boolean myDownLoad(String filePath,String fileName, String encoding, HttpServletResponse response, HttpServletRequest request){ File f = new File(filePath); if (!f.exists()) { try { response.sendError(404, "File not found!"); } catch (IOException e) { e.printStackTrace(); } return false; } String type = fileName.substring(fileName.lastIndexOf(".") + 1); //判斷下載類(lèi)型 xlsx 或 xls 現(xiàn)在只實(shí)現(xiàn)了xlsx、xls兩個(gè)類(lèi)型的文件下載 if (type.equalsIgnoreCase("xlsx") || type.equalsIgnoreCase("xls")){ response.setContentType("application/force-download;charset=UTF-8"); final String userAgent = request.getHeader("USER-AGENT"); try { if (StringUtils.contains(userAgent, "MSIE") || StringUtils.contains(userAgent, "Edge")) {// IE瀏覽器 fileName = URLEncoder.encode(fileName, "UTF8"); } else if (StringUtils.contains(userAgent, "Mozilla")) {// google,火狐瀏覽器 fileName = new String(fileName.getBytes(), "ISO8859-1"); } else { fileName = URLEncoder.encode(fileName, "UTF8");// 其他瀏覽器 } response.setHeader("Content-disposition", "attachment; filename=" + fileName); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage(), e); return false; } InputStream in = null; OutputStream out = null; try { //獲取要下載的文件輸入流 in = new FileInputStream(filePath); int len = 0; //創(chuàng)建數(shù)據(jù)緩沖區(qū) byte[] buffer = new byte[1024]; //通過(guò)response對(duì)象獲取outputStream流 out = response.getOutputStream(); //將FileInputStream流寫(xiě)入到buffer緩沖區(qū) while((len = in.read(buffer)) > 0) { //使用OutputStream將緩沖區(qū)的數(shù)據(jù)輸出到瀏覽器 out.write(buffer,0,len); } //這一步走完,將文件傳入OutputStream中后,頁(yè)面就會(huì)彈出下載框 } catch (Exception e) { logger.error(e.getMessage(), e); return false; } finally { try { if (out != null) out.close(); if(in!=null) in.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } return true; }else { logger.error("不支持的下載類(lèi)型!"); return false; } }
新聞名稱(chēng):Java后臺(tái)Controller實(shí)現(xiàn)文件下載操作-創(chuàng)新互聯(lián)
標(biāo)題來(lái)源:http://jinyejixie.com/article36/dsigpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、電子商務(wù)、軟件開(kāi)發(fā)、企業(yè)建站、定制開(kāi)發(fā)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容