版式文件批量下载

beta-enc
kane 2 years ago
parent 2b7a6c082d
commit 58c8acaef9
  1. 2
      jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java
  2. 10
      jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java
  3. 39
      jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java
  4. 6
      jianshui-storage/src/main/java/com/jianshui/storage/service/impl/CosStorageService.java

@ -28,8 +28,10 @@ public class InvoiceDownloadController {
@GetMapping("/dowmLoad/{ids}/{status}") @GetMapping("/dowmLoad/{ids}/{status}")
public Object downLoad(@PathVariable Long[] ids,@PathVariable String status,HttpServletResponse response){ public Object downLoad(@PathVariable Long[] ids,@PathVariable String status,HttpServletResponse response){
if (InvoiceCommonConstants.PDFSTATUS.equals(status)){ if (InvoiceCommonConstants.PDFSTATUS.equals(status)){
//pdf下载
return invoiceDownloadService.dowmLoadPdf(ids,response); return invoiceDownloadService.dowmLoadPdf(ids,response);
}else { }else {
//ofd下载
return invoiceDownloadService.dowmLoadOfd(ids,response); return invoiceDownloadService.dowmLoadOfd(ids,response);
} }
} }

@ -80,18 +80,22 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService {
//下载失败集合 //下载失败集合
List<DownLoadErrorVO> errorList = new ArrayList<>(); List<DownLoadErrorVO> errorList = new ArrayList<>();
for (Long id : ids) { for (Long id : ids) {
//下载传输类
DownLoadDTO downLoadDTO = new DownLoadDTO(); DownLoadDTO downLoadDTO = new DownLoadDTO();
//发票实体类
Invoice invoice = invoiceMapper.selectInvoiceById(id); Invoice invoice = invoiceMapper.selectInvoiceById(id);
//判断有无该发票
if (invoice == null){ if (invoice == null){
return AjaxResult.error(InvoiceCommonConstants.NOTINVOICEINFO); return AjaxResult.error(InvoiceCommonConstants.NOTINVOICEINFO);
} }
//获取发票pdf地址
String invoicePdfUrl = invoice.getInvoicePdfUrl(); String invoicePdfUrl = invoice.getInvoicePdfUrl();
if (invoicePdfUrl != null) { if (invoicePdfUrl != null) {
//设置下载地址类
downLoadDTO.setUrl(invoicePdfUrl); downLoadDTO.setUrl(invoicePdfUrl);
downLoadDTO.setFphm(invoice.getFphm()); downLoadDTO.setFphm(invoice.getFphm());
pdfDownLoad.add(downLoadDTO); pdfDownLoad.add(downLoadDTO);
} else { } else {
//获取pfd下载地址
//封装请求实体类 //封装请求实体类
FileAcquisitionTwoDTO fileAcquisitionDTO = new FileAcquisitionTwoDTO(); FileAcquisitionTwoDTO fileAcquisitionDTO = new FileAcquisitionTwoDTO();
//发送同步请求 //发送同步请求
@ -137,7 +141,7 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService {
} }
} }
} }
if (errorList.size() > 0 || errorList != null){ if (errorList.size() > 0){
//下载pdf文件集合 //下载pdf文件集合
BatchDownloadUtils.downloadAndZipFiles(pdfDownLoad,InvoiceCommonConstants.PDFSTATUS,response); BatchDownloadUtils.downloadAndZipFiles(pdfDownLoad,InvoiceCommonConstants.PDFSTATUS,response);
return AjaxResult.error(errorList); return AjaxResult.error(errorList);
@ -237,7 +241,7 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService {
} }
} }
} }
if (errorList.size() > 0 || errorList != null){ if (errorList.size() > 0){
//下载ofd文件集合 //下载ofd文件集合
BatchDownloadUtils.downloadAndZipFiles(ofdDownLoad,InvoiceCommonConstants.OFDSTATUS,response); BatchDownloadUtils.downloadAndZipFiles(ofdDownLoad,InvoiceCommonConstants.OFDSTATUS,response);
return AjaxResult.error(errorList); return AjaxResult.error(errorList);

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

@ -156,14 +156,14 @@ public class CosStorageService extends StorageBaseAbstractService {
throw new JianshuiServiceException("对象存储未初始化"); throw new JianshuiServiceException("对象存储未初始化");
} }
String key = IdUtils.fastSimpleUUID(); // String key = IdUtils.fastSimpleUUID();
InputStream inputStream = new ByteArrayInputStream(data); InputStream inputStream = new ByteArrayInputStream(data);
ObjectMetadata objectMetadata = new ObjectMetadata(); ObjectMetadata objectMetadata = new ObjectMetadata();
// 上传的流如果能够获取准确的流长度,则推荐一定填写 content-length // 上传的流如果能够获取准确的流长度,则推荐一定填写 content-length
// 如果确实没办法获取到,则下面这行可以省略,但同时高级接口也没办法使用分块上传了 // 如果确实没办法获取到,则下面这行可以省略,但同时高级接口也没办法使用分块上传了
objectMetadata.setContentLength(data.length); objectMetadata.setContentLength(data.length);
PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucketName, key, inputStream, objectMetadata); PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucketName, storagePath, inputStream, objectMetadata);
try { try {
PutObjectResult putObjectResult = getClient().putObject(putObjectRequest); PutObjectResult putObjectResult = getClient().putObject(putObjectRequest);
} catch (Exception e) { } catch (Exception e) {
@ -171,7 +171,7 @@ public class CosStorageService extends StorageBaseAbstractService {
} }
StorageUrlDTO storageUrlDTO = new StorageUrlDTO(); StorageUrlDTO storageUrlDTO = new StorageUrlDTO();
storageUrlDTO.setStoragePath(key); storageUrlDTO.setStoragePath(storagePath);
storageUrlDTO.setServiceProvider("cos"); storageUrlDTO.setServiceProvider("cos");
return AjaxResult.success(storageUrlDTO); return AjaxResult.success(storageUrlDTO);
} }

Loading…
Cancel
Save