|
|
@@ -1,25 +1,32 @@
|
|
|
package cn.seecoder.seecoderportalserver.serviceImpl;
|
|
|
|
|
|
-import cn.seecoder.seecoderportalcommon.vo.*;
|
|
|
+import cn.hutool.log.StaticLog;
|
|
|
+import cn.seecoder.seecoderportalcommon.vo.CourseWareVO;
|
|
|
+import cn.seecoder.seecoderportalcommon.vo.ObjectState;
|
|
|
import cn.seecoder.seecoderportalcommon.vo.dto.CourseWareDTO;
|
|
|
-import cn.seecoder.seecoderportalserver.domain.Course;
|
|
|
import cn.seecoder.seecoderportalserver.domain.CourseWare;
|
|
|
import cn.seecoder.seecoderportalserver.mapper.CourseWareMapper;
|
|
|
import cn.seecoder.seecoderportalserver.repository.CourseRepository;
|
|
|
import cn.seecoder.seecoderportalserver.repository.CourseWareRepository;
|
|
|
import cn.seecoder.seecoderportalserver.service.CourseWareService;
|
|
|
import cn.seecoder.seecoderportalserver.utility.OssUtil;
|
|
|
-import com.aliyun.oss.model.GetObjectRequest;
|
|
|
-import com.aliyun.oss.model.OSSObject;
|
|
|
-import org.checkerframework.checker.units.qual.C;
|
|
|
+import com.itextpdf.text.Document;
|
|
|
+import com.itextpdf.text.Image;
|
|
|
+import com.itextpdf.text.pdf.PdfPCell;
|
|
|
+import com.itextpdf.text.pdf.PdfPTable;
|
|
|
+import com.itextpdf.text.pdf.PdfWriter;
|
|
|
+//import org.apache.poi.hslf.usermodel.*;
|
|
|
+//import org.apache.poi.xslf.usermodel.*;
|
|
|
+import org.apache.poi.hslf.usermodel.*;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
-import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
+import java.net.URL;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -89,7 +96,15 @@ public class CourseWareServiceImpl implements CourseWareService {
|
|
|
public String uploadCourseWareFile(String name, MultipartFile file) {
|
|
|
try {
|
|
|
System.out.println("upload");
|
|
|
- System.out.println(name);
|
|
|
+// if (name.substring(name.indexOf(".")).equals(".ppt")) {
|
|
|
+// OutputStream pdfFile = null;
|
|
|
+// FileOutputStream fileOutputStream = new FileOutputStream(name.substring(0, name.indexOf(".")) + ".pdf");
|
|
|
+// pptToPdf(file.getInputStream(), fileOutputStream);
|
|
|
+// ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream();
|
|
|
+//
|
|
|
+// String courseWareUrl = ossUtil.upload(name, );
|
|
|
+//
|
|
|
+// }
|
|
|
String courseWareUrl = ossUtil.upload(name, file.getInputStream());
|
|
|
System.out.println(courseWareUrl);
|
|
|
return courseWareUrl;
|
|
|
@@ -98,43 +113,74 @@ public class CourseWareServiceImpl implements CourseWareService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// @Override
|
|
|
-// public String downloadCourseWare(HttpServletRequest req, HttpServletResponse res, Long id) throws IOException {
|
|
|
-// System.out.println("download");
|
|
|
-// CourseWare courseWare = courseWareRepository.findCourseWareById(id);
|
|
|
-// String fileName = courseWare.getName();
|
|
|
-// String filePath = courseWare.getFileUrl();
|
|
|
-//
|
|
|
-// OutputStream os = null;
|
|
|
-// InputStream is = null;
|
|
|
-// try{
|
|
|
-// res.reset();
|
|
|
-// res.setCharacterEncoding("utf-8");
|
|
|
-// res.setContentType("application/octet-stream");
|
|
|
-// res.setHeader("Content-Disposition", "attachment;" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
|
|
|
-// File f = new File(filePath);
|
|
|
-// is = new FileInputStream(f);
|
|
|
-// ServletOutputStream sout = res.getOutputStream();
|
|
|
-// BufferedInputStream bis = null;
|
|
|
-// BufferedOutputStream bos = null;
|
|
|
-// bis = new BufferedInputStream(is);
|
|
|
-// bos = new BufferedOutputStream(sout);
|
|
|
-// byte[] buff = new byte[2048];
|
|
|
-// int bytesRead;
|
|
|
-// while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
|
|
|
-// bos.write(buff, 0, bytesRead);
|
|
|
-// }
|
|
|
-// bos.flush();
|
|
|
-// bos.close();
|
|
|
-// bis.close();
|
|
|
-// is.close();
|
|
|
-// os.close();
|
|
|
-// }catch (Exception e){
|
|
|
-//
|
|
|
-// }
|
|
|
+ public static boolean pptToPdf(InputStream inputStream, OutputStream outputStream) {
|
|
|
+
|
|
|
+ Document document = null;
|
|
|
+ HSLFSlideShow hslfSlideShow = null;
|
|
|
+ PdfWriter pdfWriter = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ hslfSlideShow = new HSLFSlideShow(inputStream);
|
|
|
+
|
|
|
+ // 获取ppt文件页面
|
|
|
+ Dimension dimension = hslfSlideShow.getPageSize();
|
|
|
+
|
|
|
+ document = new Document();
|
|
|
+
|
|
|
+ // pdfWriter实例
|
|
|
+ pdfWriter = PdfWriter.getInstance(document, outputStream);
|
|
|
+
|
|
|
+ document.open();
|
|
|
+
|
|
|
+ PdfPTable pdfPTable = new PdfPTable(1);
|
|
|
+
|
|
|
+ List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();
|
|
|
+
|
|
|
+ for (int i=0; i < hslfSlideList.size(); i++) {
|
|
|
+ HSLFSlide hslfSlide = hslfSlideList.get(i);
|
|
|
+// // 设置字体, 解决中文乱码
|
|
|
+// for (HSLFShape shape : hslfSlide.getShapes()) {
|
|
|
+// HSLFTextShape textShape = (HSLFTextShape) shape;
|
|
|
//
|
|
|
-// return fileName;
|
|
|
-// }
|
|
|
+// for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
|
|
|
+// for (HSLFTextRun textRun : textParagraph.getTextRuns()) {
|
|
|
+// textRun.setFontFamily("宋体");
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
|
|
|
+
|
|
|
+ Graphics2D graphics2d = bufferedImage.createGraphics();
|
|
|
+
|
|
|
+ graphics2d.setPaint(Color.white);
|
|
|
+ graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
|
|
|
+
|
|
|
+ hslfSlide.draw(graphics2d);
|
|
|
+
|
|
|
+ graphics2d.dispose();
|
|
|
+
|
|
|
+ Image image = Image.getInstance(bufferedImage, null);
|
|
|
+ image.scalePercent(50f);
|
|
|
+
|
|
|
+ // 写入单元格
|
|
|
+ pdfPTable.addCell(new PdfPCell(image, true));
|
|
|
+ document.add(image);
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
+ if (document != null) {
|
|
|
+ document.close();
|
|
|
+ }
|
|
|
+ if (pdfWriter != null) {
|
|
|
+ pdfWriter.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("ppt转换完毕");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public String changeCourseWareName(Long id, String newName) {
|
|
|
@@ -156,4 +202,41 @@ public class CourseWareServiceImpl implements CourseWareService {
|
|
|
return courseWare.getFileUrl();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void preview(HttpServletResponse response, String filePath) {
|
|
|
+ ServletOutputStream out;
|
|
|
+ BufferedInputStream buf;
|
|
|
+ try {
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setContentType("multipart/form-data");
|
|
|
+ response.setHeader("Content-Disposition", "inline;fileName=" + filePath.substring(filePath.lastIndexOf("/") + 1));
|
|
|
+ URL url = new URL(filePath);
|
|
|
+ //使用输入读取缓冲流读取一个文件输入流
|
|
|
+ buf = new BufferedInputStream(url.openStream());
|
|
|
+ //利用response获取一个字节流输出对象
|
|
|
+ out = response.getOutputStream();
|
|
|
+ //定义个数组,由于读取缓冲流中的内容
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int n;
|
|
|
+ //while循环一直读取缓冲流中的内容到输出的对象中
|
|
|
+ while ((n = buf.read(buffer)) != -1) {
|
|
|
+ out.write(buffer, 0, n);
|
|
|
+ }
|
|
|
+ //写出到请求的地方
|
|
|
+ out.flush();
|
|
|
+ buf.close();
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ StaticLog.error("ExportFileUtil.download() IOException", e);
|
|
|
+ } catch (Exception e) {
|
|
|
+ StaticLog.error("Exception", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long getCourseIdById(Long id) {
|
|
|
+ CourseWare courseWare = courseWareRepository.findCourseWareById(id);
|
|
|
+ return courseWare.getCourseId();
|
|
|
+ }
|
|
|
+
|
|
|
}
|