亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

springboot單文件下載和多文件壓縮zip下載的實(shí)現(xiàn)

瀏覽:6日期:2023-04-11 11:01:20

單文件下載

//下載單個(gè)文件public void downloadFile(HttpServletResponse response){ String path = 'D:testce1.txt' File file = new File(path); if(file.exists()){ String fileName = file.getName(); response.setHeader('Content-Disposition', 'attachment;fileName=' + fileName); download(response,file); } }public void download(HttpServletResponse response,File file){ FileInputStream fis = null; BufferedInputStream bis = null; OutputStream os = null; try { os = response.getOutputStream(); fis = new FileInputStream(file); bis = new BufferedInputStream(fis); byte[] buffer = new byte[bis.available()]; int i = bis.read(buffer); while(i != -1){os.write(buffer, 0, i);i = bis.read(buffer); } } catch (Exception e) { e.printStackTrace(); } try { bis.close(); fis.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } }

多文件壓縮下載

//多個(gè)文件,壓縮成zip后下載public void downloadMoreFile(HttpServletResponse response) {String test1= 'D:testce1.txt'; String test2= 'D:testce2.txt'; File tfile= new File(test1); File cfile= new File(test2); List<File> files = new ArrayList<>(); files.add(tfile); files.add(cfile); if (tfile.exists() && cfile.exists()) { String zipTmp = 'D:testce1.zip'; zipd(zipTmp,files,response); } }public void zipd(String zipTmp,List<File> files,HttpServletResponse response){ File zipTmpFile = new File(zipTmp); try { if (zipTmpFile.exists()) {zipTmpFile.delete(); } zipTmpFile.createNewFile(); response.reset(); // 創(chuàng)建文件輸出流 FileOutputStream fous = new FileOutputStream(zipTmpFile); ZipOutputStream zipOut = new ZipOutputStream(fous); zipFile(files, zipOut); zipOut.close(); fous.close(); downloadZip(zipTmpFile, response); } catch (IOException e) { e.printStackTrace(); } } //files打成壓縮包 public void zipFile(List files, ZipOutputStream outputStream) { int size = files.size(); for (int i = 0; i < size; i++) { File file = (File) files.get(i); zipFile(file, outputStream); } } public void zipFile(File inputFile, ZipOutputStream ouputStream) { try { if (inputFile.exists()) {if (inputFile.isFile()) { FileInputStream IN = new FileInputStream(inputFile); BufferedInputStream bins = new BufferedInputStream(IN, 512); ZipEntry entry = new ZipEntry(inputFile.getName()); ouputStream.putNextEntry(entry); int nNumber; byte[] buffer = new byte[512]; while ((nNumber = bins.read(buffer)) != -1) { ouputStream.write(buffer, 0, nNumber); } bins.close(); IN.close();} else { try { File[] files = inputFile.listFiles(); for (int i = 0; i < files.length; i++) { zipFile(files[i], ouputStream); } } catch (Exception e) { e.printStackTrace(); }} } } catch (Exception e) { e.printStackTrace(); } }public static HttpServletResponse downloadZip(File file, HttpServletResponse response) { if (file.exists() == false) { System.out.println('待壓縮的文件目錄:' + file + '不存在.'); } else { try {// 以流的形式下載文件。InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();OutputStream toClient = new BufferedOutputStream(response.getOutputStream());response.setContentType('application/octet-stream');// 如果輸出的是中文名的文件,在此處就要用URLEncoder.encode方法進(jìn)行處理response.setHeader('Content-Disposition', 'attachment;filename=' + new String(file.getName().getBytes('GB2312'), 'ISO8859-1'));toClient.write(buffer);toClient.flush();toClient.close(); } catch (Exception ex) {ex.printStackTrace(); } finally {try { File f = new File(file.getPath()); f.delete();} catch (Exception e) { e.printStackTrace();} } } return response; }

到此這篇關(guān)于springboot單文件下載和多文件壓縮zip下載的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot文件壓縮下載內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 羞羞色院91| 视频国产91 | 8x在线成人| 欧美一级视频在线观看欧美 | 日韩欧美一区二区三区不卡在线 | 57pao国产成视频一永久免费 | 美女zw喷水视频在线观看 | 桃色播播 | 国产免费色视频 | 91精品国 | 欧洲精品在线观看 | 久久免费播放视频 | 日韩欧美三级视频 | 欧洲美女大片免费播放器视频 | 国产成人精品日本亚洲专一区 | 麻豆精品久久精品色综合 | 欧美香蕉视频在线观看 | 国产欧美一区二区三区免费 | x8x8国产在线观看2021 | 亚洲欧美综合一区二区三区四区 | 中文字字幕在线 | 国产精品入口免费麻豆 | 快猫在线观看入口免费网站满十八 | 性欧美巨大极品videos | 黄色a级网站 | 在线成年视频免费观看 | 国产精品自线在线播放 | 亚洲国产精 | 福利一区二区三区视频在线观看 | 中文字幕亚洲不卡在线亚瑟 | 欧美亚洲国产精品久久蜜芽 | 国产福利在线视频尤物tv | 被黑人做的白浆直流在线播放 | 亚洲福利午夜 | 久久久精品久久久久久久久久久 | 日日夜夜操操操 | 久久网免费视频 | 特黄a三级三级三级 | 黄色影片一级 | 91麻豆精品国产高清在线 | 一区二区三区视频观看 |