這篇文章主要介紹“怎么在java中壓縮文件并下載”,在日常操作中,相信很多人在怎么在java中壓縮文件并下載問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么在java中壓縮文件并下載”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、莒南網(wǎng)站維護(hù)、網(wǎng)站推廣。
1、ZIP文件格式
[local file header + file data + data descriptor]{1,n} + central directory + end of central directory record 即 [文件頭+文件數(shù)據(jù)+數(shù)據(jù)描述符]{此處可重復(fù)n次}+核心目錄+目錄結(jié)束標(biāo)識(shí) 當(dāng)壓縮包中有多個(gè)文件時(shí),就會(huì)有多個(gè)[文件頭+文件數(shù)據(jù)+數(shù)據(jù)描述符]
2、壓縮和下載步驟
(1)創(chuàng)建壓縮包前準(zhǔn)備
//定義壓縮包存在服務(wù)器的路徑 String path = request.getSession().getServletContext().getRealPath("/WEB-INF/fileTemp"); //創(chuàng)建路徑 File FilePath = new File(path + "/file"); if (!FilePath.exists()) { FilePath.mkdir(); } String path = FilePath.getPath() + "/"; //定義導(dǎo)出壓縮包的名稱 String title ="問價(jià)壓縮包"; //壓縮包格式 String fileNamezip = title + ".zip"; String zipPath = path + fileNamezip; //創(chuàng)建一個(gè)ZIP輸出流并實(shí)例化緩沖區(qū)域 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath))); //設(shè)置編碼格式(解決linux出現(xiàn)亂碼) out.setEncoding("gbk"); //定義字節(jié)數(shù)組 byte data[] = new byte[2048]; //獲取文件記錄(獲取文件記錄代碼省略) List FileList =。。。; if (!FileList.isEmpty()) { ExportUtil util = new ExportUtil(title,title, request, response, FilePath.getPath()); }
(2)刪除壓縮包之前的數(shù)據(jù),創(chuàng)建壓縮包
util.startZip(FilePath.getPath());
(3)循環(huán)將需要壓縮的文件放到壓縮包中
for (int i = 0; i < FileList.size(); i++) { fileVo fileVo=FileList.get(i); export(fileVo,request,response,title,FilePath.getPath(),fileName); } ------ public void export(fileVo fileVo, HttpServletRequest request, HttpServletResponse response, String title,String path, String fileName) { FileOutputStream fileOutputStream = null; try { File dirFile = null; int i = fileVo.getName().lastIndexOf("."); if(i!=-1){//存在文件類型 fileName1 = fileName1 + "." + (fileVo.getName()).substring(i+1); } boolean bFile = false; String mkdirName = path + File.separatorChar + title; dirFile = new File(mkdirName); if(!dirFile.exists()) { dirFile.getParentFile().mkdirs(); } if (dirFile.isDirectory()) { path = mkdirName + File.separatorChar + fileName1; } else { bFile = dirFile.mkdirs(); } if (bFile) { path = mkdirName + File.separatorChar + fileName1; } fileOutputStream = new FileOutputStream(path.replace("*", "")); String fileName = URLEncoder.encode(fileName1, "UTF-8"); if (fileName.length() > 110) { fileName = new String(fileName1.getBytes("gb2312"), "ISO8859-1"); } response.setHeader("Connection", "close"); response.setHeader("Content-Type", "application/octet-stream"); response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=\"" + Utf8Util.toUtf8String(fileName) + "\""); //讀取文件流輸出到到另一個(gè)位置 fileVo.getFileIo(fileOutputStream); fileOutputStream.close(); } catch (Exception e) { logger.error("異常:原因如下"+e.getMessage(), e); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e1) { // TODO Auto-generated catch block logger.error("異常:原因如下"+e1.getMessage(), e1); } } } ------
(4)壓縮完成,關(guān)閉輸出流。
util.entdZip(FilePath.getPath());
到此,關(guān)于“怎么在java中壓縮文件并下載”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
當(dāng)前標(biāo)題:怎么在java中壓縮文件并下載
文章路徑:http://muchs.cn/article28/ijoicp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、ChatGPT、App設(shè)計(jì)、網(wǎng)站制作、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)