From 168bb545efbd23fd3ee53520e4651529d9b4975f Mon Sep 17 00:00:00 2001 From: lxw <810412026@qq.com> Date: 星期五, 22 七月 2022 10:52:25 +0800 Subject: [PATCH] 文件下载 --- src/main/java/com/whyc/service/ProductBomService.java | 36 ++++++++++++++++++++++++++++++++++++ 1 files changed, 36 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/whyc/service/ProductBomService.java b/src/main/java/com/whyc/service/ProductBomService.java index a9ad87c..57693e2 100644 --- a/src/main/java/com/whyc/service/ProductBomService.java +++ b/src/main/java/com/whyc/service/ProductBomService.java @@ -2,12 +2,19 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import com.whyc.dto.FileDirPath; import com.whyc.dto.Response; +import com.whyc.dto.ZipUtils; import com.whyc.mapper.ProductBomMapper; import com.whyc.pojo.ProductBom; +import com.whyc.util.ActionUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.util.Date; import java.util.List; @Service @@ -22,4 +29,33 @@ PageInfo pageInfo=new PageInfo(list); return new Response().setII(1,list.size()>0?true:false,pageInfo,"鏁版嵁杩斿洖"); } + //鍥剧焊鏂囦欢涓嬭浇 + public void downloadCadDrawer(HttpServletRequest req, HttpServletResponse resp) { + String fileDirName = FileDirPath.getFileDirName(); + String rootFace=fileDirName+ File.separator+"face"; + String timeStr= ActionUtil.sdfwithFTP.format(new Date()); + try { + File file=new File(rootFace+".zip"); + FileOutputStream forootFace = new FileOutputStream(file); + ZipUtils.toZip(rootFace, forootFace,true); + // 杞爜闃叉涔辩爜 + resp.addHeader("Content-Disposition", "attachment;filename=" + + new String(timeStr.getBytes("UTF-8"), "ISO8859-1") + + ".zip"); + OutputStream out = resp.getOutputStream(); + FileInputStream in = new FileInputStream(rootFace+".zip"); + int len=0; + byte[] buffer =new byte[1024]; + //7. 灏嗙紦鍐插尯涓殑鏁版嵁杈撳嚭 + while ((len=in.read(buffer))>0){ + out.write(buffer,0,len); + } + in.close(); + out.close(); + } catch (FileNotFoundException | UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } } -- Gitblit v1.9.1