diff --git a/jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java b/jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java index 80f82da..af314ab 100644 --- a/jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java +++ b/jianshui-admin/src/main/java/com/jianshui/web/controller/platform/InvoiceDownloadController.java @@ -28,8 +28,10 @@ public class InvoiceDownloadController { @GetMapping("/dowmLoad/{ids}/{status}") public Object downLoad(@PathVariable Long[] ids,@PathVariable String status,HttpServletResponse response){ if (InvoiceCommonConstants.PDFSTATUS.equals(status)){ + //pdf下载 return invoiceDownloadService.dowmLoadPdf(ids,response); }else { + //ofd下载 return invoiceDownloadService.dowmLoadOfd(ids,response); } } diff --git a/jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java b/jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java index f8dddd8..e4502f5 100644 --- a/jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java +++ b/jianshui-platform/src/main/java/com/jianshui/platform/service/impl/InvoiceDownloadServiceImpl.java @@ -80,18 +80,22 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { //下载失败集合 List errorList = new ArrayList<>(); for (Long id : ids) { + //下载传输类 DownLoadDTO downLoadDTO = new DownLoadDTO(); + //发票实体类 Invoice invoice = invoiceMapper.selectInvoiceById(id); + //判断有无该发票 if (invoice == null){ return AjaxResult.error(InvoiceCommonConstants.NOTINVOICEINFO); } + //获取发票pdf地址 String invoicePdfUrl = invoice.getInvoicePdfUrl(); if (invoicePdfUrl != null) { + //设置下载地址类 downLoadDTO.setUrl(invoicePdfUrl); downLoadDTO.setFphm(invoice.getFphm()); pdfDownLoad.add(downLoadDTO); } else { - //获取pfd下载地址 //封装请求实体类 FileAcquisitionTwoDTO fileAcquisitionDTO = new FileAcquisitionTwoDTO(); //发送同步请求 @@ -137,7 +141,7 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { } } } - if (errorList.size() > 0 || errorList != null){ + if (errorList.size() > 0){ //下载pdf文件集合 BatchDownloadUtils.downloadAndZipFiles(pdfDownLoad,InvoiceCommonConstants.PDFSTATUS,response); return AjaxResult.error(errorList); @@ -237,7 +241,7 @@ public class InvoiceDownloadServiceImpl implements InvoiceDownloadService { } } } - if (errorList.size() > 0 || errorList != null){ + if (errorList.size() > 0){ //下载ofd文件集合 BatchDownloadUtils.downloadAndZipFiles(ofdDownLoad,InvoiceCommonConstants.OFDSTATUS,response); return AjaxResult.error(errorList); diff --git a/jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java b/jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java index 691281d..86f7523 100644 --- a/jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java +++ b/jianshui-platform/src/main/java/com/jianshui/platform/utils/BatchDownloadUtils.java @@ -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 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 { diff --git a/jianshui-storage/src/main/java/com/jianshui/storage/service/impl/CosStorageService.java b/jianshui-storage/src/main/java/com/jianshui/storage/service/impl/CosStorageService.java index 10bc0fe..e0d19b7 100644 --- a/jianshui-storage/src/main/java/com/jianshui/storage/service/impl/CosStorageService.java +++ b/jianshui-storage/src/main/java/com/jianshui/storage/service/impl/CosStorageService.java @@ -156,14 +156,14 @@ public class CosStorageService extends StorageBaseAbstractService { throw new JianshuiServiceException("对象存储未初始化"); } - String key = IdUtils.fastSimpleUUID(); +// String key = IdUtils.fastSimpleUUID(); InputStream inputStream = new ByteArrayInputStream(data); ObjectMetadata objectMetadata = new ObjectMetadata(); // 上传的流如果能够获取准确的流长度,则推荐一定填写 content-length // 如果确实没办法获取到,则下面这行可以省略,但同时高级接口也没办法使用分块上传了 objectMetadata.setContentLength(data.length); - PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucketName, key, inputStream, objectMetadata); + PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucketName, storagePath, inputStream, objectMetadata); try { PutObjectResult putObjectResult = getClient().putObject(putObjectRequest); } catch (Exception e) { @@ -171,7 +171,7 @@ public class CosStorageService extends StorageBaseAbstractService { } StorageUrlDTO storageUrlDTO = new StorageUrlDTO(); - storageUrlDTO.setStoragePath(key); + storageUrlDTO.setStoragePath(storagePath); storageUrlDTO.setServiceProvider("cos"); return AjaxResult.success(storageUrlDTO); }