Browse Source

处理打包错误

huangjn 2 years ago
parent
commit
0e6ecaad17

+ 5 - 0
blade-ops/blade-resource/pom.xml

@@ -100,6 +100,11 @@
             <version>5.5.13</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>com.aspose</groupId>
+            <artifactId>aspose-cells</artifactId>
+            <version>20.4</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 101 - 96
blade-ops/blade-resource/src/main/java/org/springblade/resource/endpoint/OssEndpoint.java

@@ -16,9 +16,15 @@
  */
 package org.springblade.resource.endpoint;
 
+import com.aspose.cells.SaveFormat;
+import com.itextpdf.text.Image;
+import com.itextpdf.text.Rectangle;
+import com.itextpdf.text.pdf.PdfWriter;
 import io.swagger.annotations.Api;
 import lombok.AllArgsConstructor;
 import lombok.SneakyThrows;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
 import org.springblade.core.oss.model.BladeFile;
 import org.springblade.core.oss.model.OssFile;
 import org.springblade.core.secure.annotation.PreAuth;
@@ -31,10 +37,11 @@ import org.springblade.resource.builder.oss.OssBuilder;
 import org.springblade.resource.entity.Attach;
 import org.springblade.resource.service.IAttachService;
 import org.springblade.resource.vo.NewBladeFile;
+import org.springframework.beans.BeanUtils;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
-
 import java.io.*;
+import java.util.Objects;
 
 /**
  * 对象存储端点
@@ -170,32 +177,31 @@ public class OssEndpoint {
 	@SneakyThrows
 	@PostMapping("/upload-file")
 	public R<NewBladeFile> uploadFile(@RequestParam MultipartFile file){
-//		//上传原文件
-//		BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
-//
-//		//处理PDF文件
-//		String originalFilename = "";
-//		NewBladeFile newBladeFile = new NewBladeFile();
-//		if(Objects.requireNonNull(file.getOriginalFilename()).contains("xlsx")){
-//			originalFilename = file.getOriginalFilename().replaceAll(".xlsx", ".pdf");
-//			newBladeFile = this.excelToPdf(originalFilename, file.getInputStream());
-//
-//		} else if(file.getOriginalFilename().contains("xls")){
-//			originalFilename = file.getOriginalFilename().replaceAll(".xls", ".pdf");
-//			newBladeFile = this.excelToPdf(originalFilename, file.getInputStream());
-//
-//		} else if(file.getOriginalFilename().contains("docx")){
-//			originalFilename = file.getOriginalFilename().replaceAll(".docx", ".pdf");
-//			newBladeFile = this.wordToPdf(originalFilename, file.getInputStream());
-//
-//		} else if(file.getOriginalFilename().contains("png") || file.getOriginalFilename().contains("jpg")){
-//			originalFilename = file.getOriginalFilename().replaceAll(".png", ".pdf").replaceAll(".jpg", ".pdf");
-//			newBladeFile = this.pngOrJpgToPdf(originalFilename, file.getInputStream());
-//		}
-//
-//		BeanUtils.copyProperties(bladeFile, newBladeFile);
-//		return R.data(newBladeFile);
-		return R.data(null);
+		//上传原文件
+		BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
+
+		//处理PDF文件
+		String originalFilename = "";
+		NewBladeFile newBladeFile = new NewBladeFile();
+		if(Objects.requireNonNull(file.getOriginalFilename()).contains("xlsx")){
+			originalFilename = file.getOriginalFilename().replaceAll(".xlsx", ".pdf");
+			newBladeFile = this.excelToPdf(originalFilename, file.getInputStream());
+
+		} else if(file.getOriginalFilename().contains("xls")){
+			originalFilename = file.getOriginalFilename().replaceAll(".xls", ".pdf");
+			newBladeFile = this.excelToPdf(originalFilename, file.getInputStream());
+
+		} else if(file.getOriginalFilename().contains("docx")){
+			originalFilename = file.getOriginalFilename().replaceAll(".docx", ".pdf");
+			newBladeFile = this.wordToPdf(originalFilename, file.getInputStream());
+
+		} else if(file.getOriginalFilename().contains("png") || file.getOriginalFilename().contains("jpg")){
+			originalFilename = file.getOriginalFilename().replaceAll(".png", ".pdf").replaceAll(".jpg", ".pdf");
+			newBladeFile = this.pngOrJpgToPdf(originalFilename, file.getInputStream());
+		}
+
+		BeanUtils.copyProperties(bladeFile, newBladeFile);
+		return R.data(newBladeFile);
 	}
 
 	/**
@@ -205,38 +211,38 @@ public class OssEndpoint {
 	 * @return 上传结果对象
 	 */
 	private NewBladeFile pngOrJpgToPdf(String originalFilename, InputStream is){
-//		String pdfFileUrl = "";
-//		try{
-//			com.itextpdf.text.Document document = new com.itextpdf.text.Document();
-//			document.setMargins(0,0,0,0);
-//			ByteArrayOutputStream bos = new ByteArrayOutputStream();
-//			PdfWriter.getInstance(document, bos);
-//			document.open();
-//			Image image = Image.getInstance(this.InputStreamToBytes(is));
-//			// 设置页面宽高与图片一致
-//			document.setPageSize(new Rectangle(image.getScaledWidth(), image.getScaledHeight()));
-//			// 图片居中(感觉没啥用)
-//			image.setAlignment(Image.ALIGN_CENTER);
-//			// 新建一页添加图片
-//			document.newPage();
-//			document.add(image);
-//			document.close();
-//
-//			//上传文件
-//			InputStream pdfInput = new ByteArrayInputStream(bos.toByteArray());
-//			BladeFile bladeFile = this.ossBuilder.template().putFile(originalFilename,pdfInput);
-//			pdfFileUrl = bladeFile.getLink();
-//
-//		}catch (Exception e){
-//			e.printStackTrace();
-//		}
-//
-//		NewBladeFile newBladeFile = new NewBladeFile();
-//		newBladeFile.setPdfUrl(pdfFileUrl);
-//		newBladeFile.setPage(1);
-//
-//		return newBladeFile;
-		return null;
+		String pdfFileUrl = "";
+		try{
+			com.itextpdf.text.Document document = new com.itextpdf.text.Document();
+			document.setMargins(0,0,0,0);
+			ByteArrayOutputStream bos = new ByteArrayOutputStream();
+			PdfWriter.getInstance(document, bos);
+			document.open();
+			Image image = Image.getInstance(this.InputStreamToBytes(is));
+			// 设置页面宽高与图片一致
+			document.setPageSize(new Rectangle(image.getScaledWidth(), image.getScaledHeight()));
+			// 图片居中(感觉没啥用)
+			image.setAlignment(Image.ALIGN_CENTER);
+			// 新建一页添加图片
+			document.newPage();
+			document.add(image);
+			document.close();
+
+			//上传文件
+			InputStream pdfInput = new ByteArrayInputStream(bos.toByteArray());
+			BladeFile bladeFile = this.ossBuilder.template().putFile(originalFilename,pdfInput);
+			pdfFileUrl = bladeFile.getLink();
+
+		}catch (Exception e){
+			e.printStackTrace();
+		}
+
+		NewBladeFile newBladeFile = new NewBladeFile();
+		newBladeFile.setPdfUrl(pdfFileUrl);
+		newBladeFile.setPage(1);
+
+		return newBladeFile;
+//		return null;
 	}
 
 	/**
@@ -297,43 +303,42 @@ public class OssEndpoint {
 	 * @return 上传结果对象
 	 */
 	private NewBladeFile excelToPdf(String originalFilename, InputStream is){
-//		String pdfFileUrl = "";
-//		int page = 0;
-//		try{
-//			org.apache.poi.ss.usermodel.Workbook ss = WorkbookFactory.create(is);
-//
-//			for(int i = 0, l = ss.getNumberOfSheets(); i < l; i ++){
-//				Sheet sheet = ss.getSheetAt(i);
-//				//去掉表格虚线
-//				sheet.setPrintGridlines(false);
-//				//设置 整个工作表为一页
-//				sheet.setFitToPage(true);
-//			}
-//
-//			ByteArrayOutputStream outReport = new ByteArrayOutputStream();
-//			ss.write(outReport);
-//			com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(new ByteArrayInputStream(outReport.toByteArray()));
-//
-//			ByteArrayOutputStream bos = new ByteArrayOutputStream();
-//			wb.save(bos, SaveFormat.PDF);
-//			bos.flush();
-//			//上传文件
-//			InputStream pdfInput = new ByteArrayInputStream(bos.toByteArray());
-//			BladeFile bladeFile = this.ossBuilder.template().putFile(originalFilename,pdfInput);
-//			pdfFileUrl = bladeFile.getLink();
-//
-//			//获取页数
-//			page = wb.getWorksheets().getActiveSheetIndex() + 1;
-//
-//			bos.close();
-//		}catch (Exception e){
-//			e.printStackTrace();
-//		}
-//		NewBladeFile newBladeFile = new NewBladeFile();
-//		newBladeFile.setPdfUrl(pdfFileUrl);
-//		newBladeFile.setPage(page);
-//		return  newBladeFile;
-		return null;
+		String pdfFileUrl = "";
+		int page = 0;
+		try{
+			org.apache.poi.ss.usermodel.Workbook ss = WorkbookFactory.create(is);
+
+			for(int i = 0, l = ss.getNumberOfSheets(); i < l; i ++){
+				Sheet sheet = ss.getSheetAt(i);
+				//去掉表格虚线
+				sheet.setPrintGridlines(false);
+				//设置 整个工作表为一页
+				sheet.setFitToPage(true);
+			}
+
+			ByteArrayOutputStream outReport = new ByteArrayOutputStream();
+			ss.write(outReport);
+			com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(new ByteArrayInputStream(outReport.toByteArray()));
+
+			ByteArrayOutputStream bos = new ByteArrayOutputStream();
+			wb.save(bos, SaveFormat.PDF);
+			bos.flush();
+			//上传文件
+			InputStream pdfInput = new ByteArrayInputStream(bos.toByteArray());
+			BladeFile bladeFile = this.ossBuilder.template().putFile(originalFilename,pdfInput);
+			pdfFileUrl = bladeFile.getLink();
+
+			//获取页数
+			page = wb.getWorksheets().getActiveSheetIndex() + 1;
+
+			bos.close();
+		}catch (Exception e){
+			e.printStackTrace();
+		}
+		NewBladeFile newBladeFile = new NewBladeFile();
+		newBladeFile.setPdfUrl(pdfFileUrl);
+		newBladeFile.setPage(page);
+		return  newBladeFile;
 	}
 
 	/**