|
@@ -21,6 +21,8 @@ import lombok.AllArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
+import org.apache.poi.util.IOUtils;
|
|
|
+import org.springblade.common.utils.CommonUtil;
|
|
|
import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
import org.springblade.core.log.exception.ServiceException;
|
|
|
import org.springblade.core.oss.model.BladeFile;
|
|
@@ -198,6 +200,68 @@ public class OssEndpoint {
|
|
|
return R.data(newBladeFile);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 上传文件(兼容工程文件需求)
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ @PostMapping("/upload-image-and-zip")
|
|
|
+ public R<NewBladeFile> uploadImageAndZip(@RequestParam MultipartFile file) {
|
|
|
+ //处理PDF文件
|
|
|
+ NewBladeFile newBladeFile = new NewBladeFile();
|
|
|
+ if (Objects.requireNonNull(file.getOriginalFilename()).contains("xlsx")) {
|
|
|
+ newBladeFile = this.commonFileClient.excelToPdf(file);
|
|
|
+
|
|
|
+ } else if (file.getOriginalFilename().contains("xls")) {
|
|
|
+ newBladeFile = this.commonFileClient.excelToPdf(file);
|
|
|
+
|
|
|
+ } else if (file.getOriginalFilename().contains("docx")) {
|
|
|
+ newBladeFile = this.commonFileClient.wordToPdf(file);
|
|
|
+
|
|
|
+ } else if (file.getOriginalFilename().contains("png") || file.getOriginalFilename().contains("jpg")) {
|
|
|
+ newBladeFile = this.commonFileClient.pngOrJpgToPdf(file);
|
|
|
+
|
|
|
+ }else if (file.getOriginalFilename().contains("pdf") || file.getOriginalFilename().contains("PDF")) {
|
|
|
+ newBladeFile.setPdfUrl("7");
|
|
|
+ }
|
|
|
+ if (newBladeFile == null || StringUtils.isBlank(newBladeFile.getPdfUrl())){
|
|
|
+ throw new ServiceException("图片转换PDF失败");
|
|
|
+ }
|
|
|
+ long start1 = System.currentTimeMillis();
|
|
|
+ byte[] bytes = CommonUtil.compressImage2(IOUtils.toByteArray(file.getInputStream()));
|
|
|
+ long start2 = System.currentTimeMillis();
|
|
|
+ System.out.println(start2 - start1);
|
|
|
+ //上传原图
|
|
|
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
|
|
|
+ BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), inputStream);
|
|
|
+ if (file.getOriginalFilename().contains("pdf") || file.getOriginalFilename().contains("PDF")) {
|
|
|
+ //获取PDF文件
|
|
|
+ PDDocument document = PDDocument.load(file.getInputStream());
|
|
|
+ //获取文件页数
|
|
|
+ newBladeFile.setPage(document.getPages().getCount());
|
|
|
+ //pdf的路径就是文件上传的路径
|
|
|
+ newBladeFile.setPdfUrl(bladeFile.getLink());
|
|
|
+ }
|
|
|
+
|
|
|
+ BeanUtils.copyProperties(bladeFile, newBladeFile);
|
|
|
+ newBladeFile.setFileSize(file.getSize() / 1024);
|
|
|
+ newBladeFile.setFileSizeName(formatSize(file.getSize()));
|
|
|
+ newBladeFile.setFileDate(LocalDateTime.now());
|
|
|
+ newBladeFile.setFileSuffix(bladeFile.getOriginalName().substring(bladeFile.getOriginalName().lastIndexOf(".")+1));
|
|
|
+
|
|
|
+ //入库
|
|
|
+ String fileExtension = FileUtil.getFileExtension(bladeFile.getOriginalName());
|
|
|
+ Attach attach = new Attach();
|
|
|
+ attach.setDomainUrl(bladeFile.getDomain());
|
|
|
+ attach.setLink(newBladeFile.getPdfUrl());
|
|
|
+ attach.setName(bladeFile.getName());
|
|
|
+ attach.setOriginalName(bladeFile.getOriginalName());
|
|
|
+ attach.setAttachSize(newBladeFile.getFileSize());
|
|
|
+ attach.setExtension(fileExtension);
|
|
|
+ attachService.save(attach);
|
|
|
+
|
|
|
+ return R.data(newBladeFile);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 上传文件(兼容工程文件需求)
|
|
|
*/
|