本篇文章為大家展示了如何在JavaWeb項(xiàng)目中實(shí)現(xiàn)文件壓縮下載功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
具體代碼如下所示:
//文件名稱 String[] names={"one.jpg","two.jpg","three.jpg","four.jpg"}; //四個(gè)文件流 FileInputStream input1 = new FileInputStream(new File("文件路徑")); FileInputStream input2 = new FileInputStream(new File("文件路徑")); FileInputStream input3 = new FileInputStream(new File("文件路徑")); FileInputStream input4 = new FileInputStream(new File("文件路徑")); FileInputStream[] inputs={input1,input2,input3,input4}; //ZIP打包圖片 File zipFile = new File("壓縮文件存放路徑"); byte[] buf = new byte[1024]; int len; ZipOutputStream zout=new ZipOutputStream(new FileOutputStream(zipFile)); for (int i = 0; i < inputs.length; i++) { FileInputStream in =inputs[i]; zout.putNextEntry(new ZipEntry(names[i])); while ((len = in.read(buf)) > 0) { zout.write(buf, 0, len); } zout.closeEntry(); in.close(); } zout.close(); //下載圖片 FileInputStream zipInput =new FileInputStream(zipFile); OutputStream out = response.getOutputStream(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=images.zip"); while ((len=zipInput.read(buf))!= -1){ out.write(buf,0,len); } zipInput.close(); out.flush(); out.close(); //刪除壓縮包 zipFile.delete();
上述內(nèi)容就是如何在JavaWeb項(xiàng)目中實(shí)現(xiàn)文件壓縮下載功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:如何在JavaWeb項(xiàng)目中實(shí)現(xiàn)文件壓縮下載功能-創(chuàng)新互聯(lián)
分享鏈接:http://jinyejixie.com/article14/disdde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站排名、移動(dòng)網(wǎng)站建設(shè)、企業(yè)網(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)
猜你還喜歡下面的內(nèi)容