|
@@ -16,16 +16,9 @@
|
|
|
*/
|
|
|
package org.springblade.resource.endpoint;
|
|
|
|
|
|
-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 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;
|
|
@@ -38,12 +31,10 @@ 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;
|
|
|
|
|
|
/**
|
|
|
* 对象存储端点
|
|
@@ -179,31 +170,32 @@ 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);
|
|
|
+// //上传原文件
|
|
|
+// 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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -213,37 +205,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;
|
|
|
+// 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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -266,34 +259,35 @@ public class OssEndpoint {
|
|
|
* @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;
|
|
|
+// 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;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -303,42 +297,43 @@ 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;
|
|
|
+// 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;
|
|
|
}
|
|
|
|
|
|
/**
|