|
@@ -16,19 +16,16 @@
|
|
|
*/
|
|
|
package org.springblade.resource.endpoint;
|
|
|
|
|
|
-import cn.hutool.poi.excel.WorkbookUtil;
|
|
|
-import com.aliyun.oss.OSSClient;
|
|
|
-import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
-import com.itextpdf.text.pdf.PdfReader;
|
|
|
+import com.aspose.cells.SaveFormat;
|
|
|
+import com.aspose.words.DocumentBuilder;
|
|
|
+import com.itextpdf.text.Image;
|
|
|
+import com.itextpdf.text.Rectangle;
|
|
|
+import com.itextpdf.text.pdf.PdfWriter;
|
|
|
import io.swagger.annotations.Api;
|
|
|
-import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.SneakyThrows;
|
|
|
-import org.apache.poi.POIXMLDocument;
|
|
|
-import org.apache.poi.hwpf.extractor.WordExtractor;
|
|
|
-import org.apache.poi.ss.usermodel.Workbook;
|
|
|
-import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
-import org.springblade.core.oss.AliossTemplate;
|
|
|
+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;
|
|
@@ -45,7 +42,8 @@ import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import java.io.FileInputStream;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* 对象存储端点
|
|
@@ -176,38 +174,173 @@ public class OssEndpoint {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 上传文件
|
|
|
+ * 上传文件(兼容工程文件需求)
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
@PostMapping("/upload-file")
|
|
|
public R<NewBladeFile> uploadFile(@RequestParam MultipartFile file){
|
|
|
-// BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
|
|
|
-
|
|
|
- int pageCount = 0;
|
|
|
- if(file.getOriginalFilename().contains(".docx")){
|
|
|
- XWPFDocument document = new XWPFDocument(file.getInputStream());
|
|
|
- //文件页数
|
|
|
- pageCount = document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
|
|
|
- } else if(file.getOriginalFilename().contains(".doc")){
|
|
|
- WordExtractor word = new WordExtractor(file.getInputStream());
|
|
|
- //文件页数
|
|
|
- pageCount = word.getSummaryInformation().getPageCount();
|
|
|
- } else if(file.getOriginalFilename().contains(".pdf")){
|
|
|
- PdfReader pdf = new PdfReader(file.getInputStream());
|
|
|
- pageCount = pdf.getNumberOfPages();
|
|
|
- } else if(file.getOriginalFilename().contains(".xlsx")){
|
|
|
- Workbook workbook = WorkbookUtil.createBook(file.getInputStream());
|
|
|
- } else {
|
|
|
- pageCount = 1;
|
|
|
- }
|
|
|
- //转换实体
|
|
|
+ //上传原文件
|
|
|
+ BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
|
|
|
+
|
|
|
+ //处理PDF文件
|
|
|
+ String originalFilename = "";
|
|
|
NewBladeFile newBladeFile = new NewBladeFile();
|
|
|
-// BeanUtils.copyProperties(bladeFile, newBladeFile);
|
|
|
- newBladeFile.setPage(pageCount);
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * png 和 jpg 转 pdf
|
|
|
+ * @param originalFilename 文件名称
|
|
|
+ * @param is 文件输入流
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取字节数组
|
|
|
+ */
|
|
|
+ private byte[] InputStreamToBytes(InputStream is) throws IOException {
|
|
|
+ BufferedInputStream bis = new BufferedInputStream(is);
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
+ int date = -1;
|
|
|
+ while ((date = bis.read()) != -1) {
|
|
|
+ os.write(date);
|
|
|
+ }
|
|
|
+ return os.toByteArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * word 转 pdf
|
|
|
+ * @param originalFilename 文件名称
|
|
|
+ * @param is 文件输入流
|
|
|
+ * @return 上传结果对象
|
|
|
+ */
|
|
|
+ private NewBladeFile wordToPdf(String originalFilename, InputStream is){
|
|
|
+ String pdfFileUrl = "";
|
|
|
+ int page = 0;
|
|
|
+ try{
|
|
|
+ com.aspose.words.Document document = new com.aspose.words.Document(is);
|
|
|
+
|
|
|
+ DocumentBuilder documentBuilder = new DocumentBuilder(document);
|
|
|
+ com.aspose.words.Font font = documentBuilder.getFont();
|
|
|
+
|
|
|
+ font.setName("宋体");
|
|
|
+
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ document.save(bos,com.aspose.words.SaveFormat.PDF);
|
|
|
+
|
|
|
+ //上传文件
|
|
|
+ InputStream pdfInput = new ByteArrayInputStream(bos.toByteArray());
|
|
|
+ BladeFile bladeFile = this.ossBuilder.template().putFile(originalFilename,pdfInput);
|
|
|
+ pdfFileUrl = bladeFile.getLink();
|
|
|
+
|
|
|
+ //获取页数
|
|
|
+ page = document.getPageCount();
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ NewBladeFile newBladeFile = new NewBladeFile();
|
|
|
+ newBladeFile.setPdfUrl(pdfFileUrl);
|
|
|
+ newBladeFile.setPage(page);
|
|
|
+ return newBladeFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * excel 转 pdf
|
|
|
+ * @param originalFilename 文件名称
|
|
|
+ * @param is 文件输入流
|
|
|
+ * @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;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 上传文件
|
|
|
*
|