介绍
今天就跟大家聊聊有关使用springboot如何实现下载单或多的zip文件,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
单文件下载
//下载单个文件 公共空间downloadFile (HttpServletResponse响应){ 字符串路径=癉: \ \ ce \ 1. txt"测试; 文件文件=新文件(路径); 如果(file.exists ()) { 字符串文件名=file.getName (); response.setHeader (“Content-Disposition",“附件文件名=?+文件名); 下载(响应文件); } } 公共空间下载(HttpServletResponse响应文件文件){ FileInputStream fis=零; BufferedInputStream bis=零; OutputStream os=零; 尝试{ 操作系统=response.getOutputStream (); fis=new FileInputStream(文件); bis=new BufferedInputStream (fis); byte[]缓冲=new byte [bis.available ()); int i=bis.read(缓冲); 虽然(我!=1){ 操作系统。写(缓冲,0,我); i=bis.read(缓冲); } }捕捉(异常e) { e.printStackTrace (); } 尝试{ bis.close (); fis.close (); os.close (); }捕捉(IOException e) { e.printStackTrace (); } }
多文件压缩下载
//多个文件,压缩成zip后下载 公共空间downloadMoreFile (HttpServletResponse响应){ 测试字符串test1=癉: \ \ ce \ 1. txt"; 测试字符串test2=癉: \ \ ce \ 2. txt"; 文件tfile=新文件(test1); 文件用=新文件(test2); List文件=new ArrayList<在(); files.add (tfile); files.add(用); 如果(tfile.exists (),,cfile.exists ()) { 测试字符串zipTmp=癉: \ \ ce \ 1. zip"; zipd (zipTmp、文件响应); } } 公共空间zipd (String zipTmp List 文件,HttpServletResponse响应){ 文件zipTmpFile=新文件(zipTmp); 尝试{ 如果(zipTmpFile.exists ()) { zipTmpFile.delete (); } zipTmpFile.createNewFile (); response.reset ();//创建文件输出流 FileOutputStream入手=new FileOutputStream (zipTmpFile); ZipOutputStream zipOut=new ZipOutputStream(入手); zipFile(文件、zipOut); zipOut.close (); fous.close (); downloadZip (zipTmpFile、响应); }捕捉(IOException e) { e.printStackTrace (); } }//文件打成压缩包 公共空间zipFile(文件列表,ZipOutputStream outputStream) { int大?files.size (); for (int i=0;我& lt;大小;我+ +){ 文件文件=(文件)files.get(我); zipFile(文件,outputStream); } } 公共空间zipFile(文件inputFile ZipOutputStream ouputStream) { 尝试{ 如果(inputFile.exists ()) { 如果(inputFile.isFile ()) { FileInputStream=新FileInputStream (inputFile); BufferedInputStream垃圾箱=new BufferedInputStream (512); ZipEntry入口=new ZipEntry (inputFile.getName ()); ouputStream.putNextEntry(入口); int nNumber; byte[]新字节缓冲区=[512]; 在((nNumber=bins.read(缓冲)!=1){ ouputStream。写(缓冲区,0,nNumber); } bins.close (); IN.close (); 其他}{ 尝试{ 文件[]文件=inputFile.listFiles (); for (int i=0;我& lt;files.length;我+ +){ zipFile(文件[我],ouputStream); } }捕捉(异常e) { e.printStackTrace (); } } } }捕捉(异常e) { e.printStackTrace (); } } 公共静态HttpServletResponse downloadZip(文件文件,HttpServletResponse响应){ 如果(file.exists ()==false) { System.out.println(“待压缩的文件目录:“+文件+“不存在!”); 其他}{ 尝试{//以流的形式下载文件。 InputStream fis=new BufferedInputStream(新FileInputStream (file.getPath ())); byte[]缓冲=new byte [fis.available ()); fis.read(缓冲); fis.close ();//清空响应 response.reset (); OutputStream toClient=new BufferedOutputStream (response.getOutputStream ()); response.setContentType(“应用程序/octet-stream");//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理 response.setHeader (“Content-Disposition" “附件文件名=?+新的字符串(file.getName () .getBytes (“GB2312"),“ISO8859-1")); toClient.write(缓冲); toClient.flush (); toClient.close (); }捕捉(例外的前女友){ ex.printStackTrace (); 最后}{ 尝试{ 文件f=新文件(file.getPath ()); f.delete (); }捕捉(异常e) { e.printStackTrace (); } } } 返回响应; } 使用springboot如何实现下载单或多的zip文件