|
@@ -37,8 +37,10 @@ import org.springblade.core.tool.utils.FileUtil;
|
|
import org.springblade.core.tool.utils.Func;
|
|
import org.springblade.core.tool.utils.Func;
|
|
import org.springblade.resource.builder.oss.OssBuilder;
|
|
import org.springblade.resource.builder.oss.OssBuilder;
|
|
import org.springblade.resource.entity.Attach;
|
|
import org.springblade.resource.entity.Attach;
|
|
|
|
+import org.springblade.resource.feign.CommonFileClient;
|
|
import org.springblade.resource.service.IAttachService;
|
|
import org.springblade.resource.service.IAttachService;
|
|
import org.springblade.resource.vo.NewBladeFile;
|
|
import org.springblade.resource.vo.NewBladeFile;
|
|
|
|
+import org.springblade.resource.vo.ToPdfVO;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -67,6 +69,8 @@ public class OssEndpoint {
|
|
*/
|
|
*/
|
|
private final IAttachService attachService;
|
|
private final IAttachService attachService;
|
|
|
|
|
|
|
|
+ private final CommonFileClient commonFileClient;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 创建存储桶
|
|
* 创建存储桶
|
|
*
|
|
*
|
|
@@ -183,23 +187,18 @@ public class OssEndpoint {
|
|
BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
|
|
BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
|
|
|
|
|
|
//处理PDF文件
|
|
//处理PDF文件
|
|
- String originalFilename = "";
|
|
|
|
NewBladeFile newBladeFile = new NewBladeFile();
|
|
NewBladeFile newBladeFile = new NewBladeFile();
|
|
if(Objects.requireNonNull(file.getOriginalFilename()).contains("xlsx")){
|
|
if(Objects.requireNonNull(file.getOriginalFilename()).contains("xlsx")){
|
|
- originalFilename = file.getOriginalFilename().replaceAll(".xlsx", ".pdf");
|
|
|
|
- newBladeFile = this.excelToPdf(originalFilename, file.getInputStream());
|
|
|
|
|
|
+ newBladeFile = this.commonFileClient.excelToPdf(file);
|
|
|
|
|
|
} else if(file.getOriginalFilename().contains("xls")){
|
|
} else if(file.getOriginalFilename().contains("xls")){
|
|
- originalFilename = file.getOriginalFilename().replaceAll(".xls", ".pdf");
|
|
|
|
- newBladeFile = this.excelToPdf(originalFilename, file.getInputStream());
|
|
|
|
|
|
+ newBladeFile = this.commonFileClient.excelToPdf(file);
|
|
|
|
|
|
} else if(file.getOriginalFilename().contains("docx")){
|
|
} else if(file.getOriginalFilename().contains("docx")){
|
|
- originalFilename = file.getOriginalFilename().replaceAll(".docx", ".pdf");
|
|
|
|
- newBladeFile = this.wordToPdf(originalFilename, file.getInputStream());
|
|
|
|
|
|
+ newBladeFile = this.commonFileClient.wordToPdf(file);
|
|
|
|
|
|
} else if(file.getOriginalFilename().contains("png") || file.getOriginalFilename().contains("jpg")){
|
|
} 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());
|
|
|
|
|
|
+ newBladeFile = this.commonFileClient.pngOrJpgToPdf(file);
|
|
|
|
|
|
} else if(file.getOriginalFilename().contains("pdf")){
|
|
} else if(file.getOriginalFilename().contains("pdf")){
|
|
//获取PDF文件
|
|
//获取PDF文件
|
|
@@ -214,140 +213,140 @@ public class OssEndpoint {
|
|
return R.data(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;
|
|
|
|
- }
|
|
|
|
|
|
+// /**
|
|
|
|
+// * 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;
|
|
|
|
+// }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 上传文件
|
|
* 上传文件
|