|
@@ -12,16 +12,21 @@ import com.hssx.cloudmodel.service.MouldFileService;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.hssx.cloudmodel.util.FileUtil;
|
|
|
import com.hssx.cloudmodel.util.HttpRespMsg;
|
|
|
+import com.hssx.cloudmodel.util.ListUtil;
|
|
|
import com.hssx.cloudmodel.util.PageUtil;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
/**
|
|
@@ -300,7 +305,89 @@ public class MouldFileServiceImpl extends ServiceImpl<MouldFileMapper, MouldFile
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public HttpRespMsg dowloadFileList(UserVO userVO) {
|
|
|
+ public HttpRespMsg dowloadFileList(UserVO userVO, HttpServletRequest request, HttpServletResponse response,String downloadPath) throws IOException {
|
|
|
+ HttpRespMsg msg = new HttpRespMsg();
|
|
|
+ if(null != userVO.getIds()){
|
|
|
+ List<Integer> ids = ListUtil.convertIntegerIdsArrayToList(userVO.getIds());
|
|
|
+ for (Integer id : ids) {
|
|
|
+ Mould mould = mouldMapper.selectById(id);
|
|
|
+ List<MouldFile> mouldFiles = mouldFileMapper.selectList(new QueryWrapper<MouldFile>().eq("model_id", id).eq("state",3));
|
|
|
+ feedBackDirectMultiDownload(request,response,downloadPath,mould,mouldFiles);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String,Object> feedBackDirectMultiDownload(HttpServletRequest request, HttpServletResponse response,String downloadPath,Mould vo,List<MouldFile> mouldFiles) throws IOException {
|
|
|
+ //压缩文件初始设置
|
|
|
+ String path= downloadPath;
|
|
|
+ String base_name = vo.getModelNo()+vo.getModelName();
|
|
|
+ String fileZip = base_name + ".zip"; // 拼接zip文件
|
|
|
+ String filePath = path+"\\" + fileZip;//之后用来生成zip文件
|
|
|
+
|
|
|
+ //mouldFiles为根据前台传过来的信息,通过数据库查询所得出的pdf文件路径集合(具体到后缀),此处省略
|
|
|
+ File[] files = new File[mouldFiles.size()];//
|
|
|
+ for(int i=0;i<mouldFiles.size();i++){
|
|
|
+ files[i]=new File(mouldFiles.get(i).getFileUrl());//获取所有需要下载的pdf
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建临时压缩文件
|
|
|
+ try {
|
|
|
+ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
|
|
|
+ ZipOutputStream zos = new ZipOutputStream(bos);
|
|
|
+ ZipEntry ze = null;
|
|
|
+ for (int i = 0; i < files.length; i++) {//将所有需要下载的pdf文件都写入临时zip文件
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files[i]));
|
|
|
+ ze = new ZipEntry(files[i].getName());
|
|
|
+ zos.putNextEntry(ze);
|
|
|
+ int s = -1;
|
|
|
+ while ((s = bis.read()) != -1) {
|
|
|
+ zos.write(s);
|
|
|
+ }
|
|
|
+ bis.close();
|
|
|
+ }
|
|
|
+ zos.flush();
|
|
|
+ zos.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //以上,临时压缩文件创建完成
|
|
|
+ //进行浏览器下载
|
|
|
+ //获得浏览器代理信息
|
|
|
+ final String userAgent = request.getHeader("USER-AGENT");
|
|
|
+ //判断浏览器代理并分别设置响应给浏览器的编码格式
|
|
|
+ String finalFileName = null;
|
|
|
+ if(StringUtils.contains(userAgent, "MSIE")||StringUtils.contains(userAgent,"Trident")){//IE浏览器
|
|
|
+ finalFileName = URLEncoder.encode(fileZip,"UTF8");
|
|
|
+ System.out.println("IE浏览器");
|
|
|
+ }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
|
|
|
+ finalFileName = new String(fileZip.getBytes(), "ISO8859-1");
|
|
|
+ }else{
|
|
|
+ finalFileName = URLEncoder.encode(fileZip,"UTF8");//其他浏览器
|
|
|
+ }
|
|
|
+ response.setContentType("application/x-download");//告知浏览器下载文件,而不是直接打开,浏览器默认为打开
|
|
|
+ response.setHeader("Content-Disposition" ,"attachment;filename=\"" +finalFileName+ "\"");//下载文件的名称
|
|
|
+
|
|
|
+ ServletOutputStream servletOutputStream=response.getOutputStream();
|
|
|
+ DataOutputStream temps = new DataOutputStream(
|
|
|
+ servletOutputStream);
|
|
|
+
|
|
|
+ DataInputStream in = new DataInputStream(new FileInputStream(filePath));//浏览器下载文件的路径
|
|
|
+ byte[] b = new byte[2048];
|
|
|
+ File reportZip=new File(filePath);//之后用来删除临时压缩文件
|
|
|
+ try {
|
|
|
+ while ((in.read(b)) != -1) {
|
|
|
+ temps.write(b);
|
|
|
+ }
|
|
|
+ temps.flush();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }finally{
|
|
|
+ if(temps!=null) temps.close();
|
|
|
+ if(in!=null) in.close();
|
|
|
+ if(reportZip!=null) reportZip.delete();//删除服务器本地产生的临时压缩文件
|
|
|
+ servletOutputStream.close();
|
|
|
+ }
|
|
|
return null;
|
|
|
}
|
|
|
}
|