|
@@ -3,63 +3,87 @@ package org.springblade.business.utils;
|
|
|
import com.itextpdf.text.Document;
|
|
|
import com.itextpdf.text.pdf.PdfCopy;
|
|
|
import com.itextpdf.text.pdf.PdfReader;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.poi.ss.usermodel.ClientAnchor;
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
import org.apache.poi.util.Units;
|
|
|
import org.springblade.common.utils.CommonUtil;
|
|
|
import org.springblade.common.vo.DataVO;
|
|
|
+import org.springblade.core.tool.utils.IoUtil;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.Arrays;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
import java.util.regex.Matcher;
|
|
|
+import java.util.zip.ZipEntry;
|
|
|
+import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
public class FileUtils {
|
|
|
|
|
|
- public Map<String,Integer> getDxDy(Sheet sheet, ClientAnchor anchor){
|
|
|
- Map<String,Integer> map = new HashMap<>();
|
|
|
- if(sheet != null && anchor != null){
|
|
|
- /*左上角单元格坐标*/
|
|
|
- final int x1=anchor.getCol1();
|
|
|
- final int y1=anchor.getRow1();
|
|
|
- /*相对位移像素*/
|
|
|
- final int dx1= Units.pointsToPixel(Units.toPoints(anchor.getDx1()));
|
|
|
- final int dy1= Units.pointsToPixel(Units.toPoints(anchor.getDy1()));
|
|
|
- int sum1=0;
|
|
|
- for(int i=0;i<x1;i++){
|
|
|
- sum1+=sheet.getColumnWidthInPixels(i)+8;
|
|
|
- }
|
|
|
- sum1+=dx1;
|
|
|
- map.put("x1",sum1);
|
|
|
- int sum2=0;
|
|
|
- for(int i=0;i<y1;i++){
|
|
|
- sum2+=Units.pointsToPixel(sheet.getRow(i).getHeightInPoints());
|
|
|
+ public static void batchDownloadFileToZip(List<String> urls, HttpServletResponse response){
|
|
|
+ // 设置压缩流:直接写入response,实现边压缩边下载
|
|
|
+ ZipOutputStream zipos = null;
|
|
|
+ // 循环将文件写入压缩流
|
|
|
+ DataOutputStream os = null;
|
|
|
+ try{
|
|
|
+
|
|
|
+ // 响应头的设置
|
|
|
+ response.reset();
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentType("multipart/form-data");
|
|
|
+ // 设置压缩包的名字
|
|
|
+ // 解决不同浏览器压缩包名字含有中文时乱码的问题
|
|
|
+ String downloadName = "附件-" + System.currentTimeMillis() + ".zip";
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(downloadName, "UTF-8"));
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));
|
|
|
+ // 设置压缩方法
|
|
|
+ zipos.setMethod(ZipOutputStream.DEFLATED);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
- sum2+=dy1;
|
|
|
- map.put("y1",sum2);
|
|
|
- /*右下角单元格坐标*/
|
|
|
- final int x2=anchor.getCol2();
|
|
|
- final int y2=anchor.getRow2();
|
|
|
- /*相对位移像素*/
|
|
|
- final int dx2= Units.pointsToPixel(Units.toPoints(anchor.getDx2()));
|
|
|
- final int dy2= Units.pointsToPixel(Units.toPoints(anchor.getDy2()));
|
|
|
- int sum3=0;
|
|
|
- for(int i=0;i<x2;i++){
|
|
|
- sum3+=sheet.getColumnWidthInPixels(i)+8;
|
|
|
+
|
|
|
+ if(zipos == null){
|
|
|
+ return;
|
|
|
}
|
|
|
- sum3+=dx2;
|
|
|
- map.put("x2",sum3);
|
|
|
- int sum4=0;
|
|
|
- for(int i=0;i<y2;i++){
|
|
|
- sum4+=Units.pointsToPixel(sheet.getRow(i).getHeightInPoints());
|
|
|
+
|
|
|
+ InputStream inputStream = null;
|
|
|
+
|
|
|
+ for(String url : urls){
|
|
|
+ if(StringUtils.isNotEmpty(url)){
|
|
|
+ try{
|
|
|
+
|
|
|
+ //获取文件流
|
|
|
+ inputStream = CommonUtil.getOSSInputStream(url);
|
|
|
+ //转换
|
|
|
+ byte[] bytes = CommonUtil.InputStreamToBytes(inputStream);
|
|
|
+
|
|
|
+ String fileName = url.substring(url.lastIndexOf("/") + 1);
|
|
|
+ zipos.putNextEntry(new ZipEntry(fileName));
|
|
|
+
|
|
|
+ os = new DataOutputStream(zipos);
|
|
|
+ os.write(bytes);
|
|
|
+ zipos.closeEntry();
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ IoUtil.closeQuietly(inputStream);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- sum4+=dy2;
|
|
|
- map.put("y2",sum4);
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ IoUtil.closeQuietly(os);
|
|
|
+ IoUtil.closeQuietly(zipos);
|
|
|
}
|
|
|
- return map;
|
|
|
}
|
|
|
|
|
|
/**
|