|
|
|
@ -1,12 +1,8 @@ |
|
|
|
|
package com.jianshui.platform.utils; |
|
|
|
|
|
|
|
|
|
import com.jianshui.common.constant.InvoiceCommonConstants; |
|
|
|
|
import com.jianshui.invoice.domain.Invoice; |
|
|
|
|
import com.jianshui.invoice.mapper.InvoiceMapper; |
|
|
|
|
import com.jianshui.platform.dto.DownLoadDTO; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.InputStream; |
|
|
|
@ -14,10 +10,8 @@ import java.io.OutputStream; |
|
|
|
|
import java.net.HttpURLConnection; |
|
|
|
|
import java.net.URL; |
|
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.UUID; |
|
|
|
|
import java.util.zip.ZipEntry; |
|
|
|
|
import java.util.zip.ZipOutputStream; |
|
|
|
|
|
|
|
|
@ -32,11 +26,12 @@ public class BatchDownloadUtils { |
|
|
|
|
/** |
|
|
|
|
* 功能描述: 批量下载 |
|
|
|
|
* |
|
|
|
|
* @param status 文件类型0-pfd 1-ofd |
|
|
|
|
* @param status 文件类型0-pdf 1-ofd |
|
|
|
|
* @param downLoad |
|
|
|
|
* @return : void |
|
|
|
|
*/ |
|
|
|
|
public static void downloadAndZipFiles(List<DownLoadDTO> downLoad, String status, HttpServletResponse response) { |
|
|
|
|
//获取当前时间为包名
|
|
|
|
|
Date now = new Date(); |
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat(InvoiceCommonConstants.DATEFORMAT); |
|
|
|
|
String name = format.format(now); |
|
|
|
@ -45,6 +40,7 @@ public class BatchDownloadUtils { |
|
|
|
|
ZipOutputStream zos = null; |
|
|
|
|
InputStream inputStream = null; |
|
|
|
|
try { |
|
|
|
|
//设置响应配置
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + zipFileName); |
|
|
|
|
response.setCharacterEncoding("utf-8"); |
|
|
|
|
ops = response.getOutputStream(); |
|
|
|
@ -53,11 +49,16 @@ public class BatchDownloadUtils { |
|
|
|
|
for (DownLoadDTO downLoadDTO : downLoad) { |
|
|
|
|
String url = downLoadDTO.getUrl(); |
|
|
|
|
if (StringUtils.hasText(url)) { |
|
|
|
|
//将字符串地址转为url下载
|
|
|
|
|
URL downloadUrl = new URL(url); |
|
|
|
|
//创建下载连接
|
|
|
|
|
HttpURLConnection httpConn = (HttpURLConnection) downloadUrl.openConnection(); |
|
|
|
|
//获取下载状态码
|
|
|
|
|
int responseCode = httpConn.getResponseCode(); |
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) { |
|
|
|
|
fileName = getFileName(httpConn, status,downLoadDTO.getFphm()); |
|
|
|
|
//获取下载后文件名称
|
|
|
|
|
fileName = getFileName(httpConn, status, downLoadDTO.getFphm()); |
|
|
|
|
//读取流文件
|
|
|
|
|
inputStream = httpConn.getInputStream(); |
|
|
|
|
ZipEntry zipEntry = new ZipEntry(fileName); |
|
|
|
|
zos.putNextEntry(zipEntry); |
|
|
|
@ -67,6 +68,24 @@ public class BatchDownloadUtils { |
|
|
|
|
zos.write(buffer, 0, bytesRead); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//状态码为302重定向
|
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP) { |
|
|
|
|
//获取重定向后的下载地址
|
|
|
|
|
String newUrl = httpConn.getHeaderField("Location"); |
|
|
|
|
URL urlNew = new URL(newUrl); |
|
|
|
|
HttpURLConnection httpConnNew = (HttpURLConnection) urlNew.openConnection(); |
|
|
|
|
fileName = getFileName(httpConnNew, status, downLoadDTO.getFphm()); |
|
|
|
|
inputStream = httpConnNew.getInputStream(); |
|
|
|
|
ZipEntry zipEntry = new ZipEntry(fileName); |
|
|
|
|
zos.putNextEntry(zipEntry); |
|
|
|
|
int bytesRead = -1; |
|
|
|
|
byte[] buffer = new byte[4096]; |
|
|
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) { |
|
|
|
|
zos.write(buffer, 0, bytesRead); |
|
|
|
|
} |
|
|
|
|
httpConnNew.disconnect(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
httpConn.disconnect(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -104,13 +123,12 @@ public class BatchDownloadUtils { |
|
|
|
|
* @param httpConn |
|
|
|
|
* @return : java.lang.String |
|
|
|
|
*/ |
|
|
|
|
private static String getFileName(HttpURLConnection httpConn, String status,String fphm) { |
|
|
|
|
private static String getFileName(HttpURLConnection httpConn, String status, String fphm) { |
|
|
|
|
String fileName = ""; |
|
|
|
|
String disposition = httpConn.getHeaderField("Content-Disposition"); |
|
|
|
|
if (disposition != null) { |
|
|
|
|
int index = disposition.indexOf("filename="); |
|
|
|
|
if (index > 0) { |
|
|
|
|
fileName = disposition.substring(index + 10, disposition.length() - 1); |
|
|
|
|
if (InvoiceCommonConstants.PDFSTATUS.equals(status)) { |
|
|
|
|
fileName = fphm + ".pdf"; |
|
|
|
|
} else { |
|
|
|
@ -119,7 +137,6 @@ public class BatchDownloadUtils { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (StringUtils.isEmpty(fileName)) { |
|
|
|
|
fileName = httpConn.getURL().getFile().substring(httpConn.getURL().getFile().lastIndexOf("/") + 1); |
|
|
|
|
if (InvoiceCommonConstants.PDFSTATUS.equals(status)) { |
|
|
|
|
fileName = fphm + ".pdf"; |
|
|
|
|
} else { |
|
|
|
|