您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)使用springboot如何實現(xiàn)下載單或多的zip文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
單文件下載
//下載單個文件 public void downloadFile(HttpServletResponse response){ String path = "D:\test\ce\1.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(); } }
多文件壓縮下載
//多個文件,壓縮成zip后下載 public void downloadMoreFile(HttpServletResponse response) { String test1= "D:\test\ce\1.txt"; String test2= "D:\test\ce\2.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:\test\ce\1.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(); // 清空response response.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; }
看完上述內(nèi)容,你們對使用springboot如何實現(xiàn)下載單或多的zip文件有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。