|
@@ -1,7 +1,13 @@
|
|
package com.caimei.modules.archive.service;
|
|
package com.caimei.modules.archive.service;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
|
|
+import java.awt.*;
|
|
|
|
+import java.awt.geom.Rectangle2D;
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.net.URI;
|
|
|
|
+import java.net.URL;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
+import java.util.List;
|
|
|
|
|
|
import com.caimei.dfs.image.beens.ImageUploadInfo;
|
|
import com.caimei.dfs.image.beens.ImageUploadInfo;
|
|
import com.caimei.modules.archive.entity.CmOrderArchiveFile;
|
|
import com.caimei.modules.archive.entity.CmOrderArchiveFile;
|
|
@@ -12,6 +18,15 @@ import com.caimei.modules.sys.utils.UploadImageUtils;
|
|
import com.thinkgem.jeesite.common.config.Global;
|
|
import com.thinkgem.jeesite.common.config.Global;
|
|
import com.thinkgem.jeesite.common.utils.Encodes;
|
|
import com.thinkgem.jeesite.common.utils.Encodes;
|
|
import com.thinkgem.jeesite.common.utils.StringUtils;
|
|
import com.thinkgem.jeesite.common.utils.StringUtils;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+import org.apache.poi.hslf.model.TextRun;
|
|
|
|
+import org.apache.poi.hslf.usermodel.RichTextRun;
|
|
|
|
+import org.apache.poi.hslf.usermodel.SlideShow;
|
|
|
|
+import org.apache.poi.hwpf.HWPFDocument;
|
|
|
|
+import org.apache.poi.hwpf.converter.PicturesManager;
|
|
|
|
+import org.apache.poi.hwpf.converter.WordToHtmlConverter;
|
|
|
|
+import org.apache.poi.hwpf.usermodel.Picture;
|
|
|
|
+import org.apache.poi.hwpf.usermodel.PictureType;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -20,8 +35,16 @@ import com.thinkgem.jeesite.common.service.CrudService;
|
|
import com.caimei.modules.archive.entity.CmProductArchiveContent;
|
|
import com.caimei.modules.archive.entity.CmProductArchiveContent;
|
|
import com.caimei.modules.archive.dao.CmProductArchiveContentDao;
|
|
import com.caimei.modules.archive.dao.CmProductArchiveContentDao;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+import org.w3c.dom.Document;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
+import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
+import javax.xml.transform.OutputKeys;
|
|
|
|
+import javax.xml.transform.Transformer;
|
|
|
|
+import javax.xml.transform.TransformerFactory;
|
|
|
|
+import javax.xml.transform.dom.DOMSource;
|
|
|
|
+import javax.xml.transform.stream.StreamResult;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 商品资料内容Service
|
|
* 商品资料内容Service
|
|
@@ -37,7 +60,9 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
|
|
public CmProductArchiveContent get(String id) {
|
|
public CmProductArchiveContent get(String id) {
|
|
return super.get(id);
|
|
return super.get(id);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ private final String htmlTempPath = Global.getConfig("archive.htmlTempPath");
|
|
|
|
+
|
|
public List<CmProductArchiveContent> findList(CmProductArchiveContent cmProductArchiveContent) {
|
|
public List<CmProductArchiveContent> findList(CmProductArchiveContent cmProductArchiveContent) {
|
|
return super.findList(cmProductArchiveContent);
|
|
return super.findList(cmProductArchiveContent);
|
|
}
|
|
}
|
|
@@ -57,7 +82,15 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
|
|
imageList.forEach(image -> {
|
|
imageList.forEach(image -> {
|
|
if (StringUtils.isNotEmpty(image.getOssUrl())) {
|
|
if (StringUtils.isNotEmpty(image.getOssUrl())) {
|
|
if (!image.getOssUrl().startsWith("http")) {
|
|
if (!image.getOssUrl().startsWith("http")) {
|
|
- image.setOssUrl(getImageUrl(image.getOssUrl()));
|
|
|
|
|
|
+ String imageUrl = getImageUrl(image.getOssUrl());
|
|
|
|
+ try {
|
|
|
|
+ //获取图片的size
|
|
|
|
+ BufferedImage buffImg = ImageIO.read(new URL(imageUrl));
|
|
|
|
+ imageUrl += "?width=" + buffImg.getWidth() + "&height=" + buffImg.getHeight();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ image.setOssUrl(imageUrl);
|
|
}
|
|
}
|
|
image.setArchiveContentId(Integer.parseInt(cmProductArchiveContent.getId()));
|
|
image.setArchiveContentId(Integer.parseInt(cmProductArchiveContent.getId()));
|
|
cmProductArchiveContentDao.insertFile(image);
|
|
cmProductArchiveContentDao.insertFile(image);
|
|
@@ -112,22 +145,34 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
|
|
Map<String, Object> map = new HashMap<>();
|
|
Map<String, Object> map = new HashMap<>();
|
|
String fileAllName = multipartFile.getOriginalFilename();
|
|
String fileAllName = multipartFile.getOriginalFilename();
|
|
String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1);
|
|
String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1);
|
|
|
|
+ String fileShortName = fileAllName.substring(0, fileAllName.lastIndexOf("."));
|
|
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
String filePath = uuid + "." + fileType;
|
|
String filePath = uuid + "." + fileType;
|
|
String contentType = OssArchiveUtil.getContentType(fileAllName);
|
|
String contentType = OssArchiveUtil.getContentType(fileAllName);
|
|
try {
|
|
try {
|
|
//保存本地
|
|
//保存本地
|
|
File file = OssArchiveUtil.ossUpload(multipartFile);
|
|
File file = OssArchiveUtil.ossUpload(multipartFile);
|
|
- logger.info("默认路径>>>" + file.getAbsolutePath());
|
|
|
|
|
|
+ logger.info("默认路径>>>" + file.getAbsolutePath());
|
|
//上传oss
|
|
//上传oss
|
|
String url = OssArchiveUtil.ossUpload(filePath, "archiveFile/", file, contentType);
|
|
String url = OssArchiveUtil.ossUpload(filePath, "archiveFile/", file, contentType);
|
|
- //删除本地文件
|
|
|
|
- OssArchiveUtil.deleteFile(file);
|
|
|
|
|
|
+ String htmlUrl = null;
|
|
|
|
+ //doc和ppt文件生成html文件
|
|
|
|
+ /*if (fileType.contains("doc") || fileType.contains("ppt")) {
|
|
|
|
+ String htmluuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
|
+ String htmlFilePath = htmluuid + ".html";
|
|
|
|
+ String htmlPath = generateHtml(file, fileShortName, fileType);
|
|
|
|
+ File htmlFile = new File(htmlPath);
|
|
|
|
+ htmlUrl = OssArchiveUtil.ossUpload(htmlFilePath, "archiveFile/", htmlFile, "text/html");
|
|
|
|
+ htmlFile.delete();
|
|
|
|
+ }*/
|
|
|
|
+ //删除本地文件
|
|
|
|
+ OssArchiveUtil.deleteFile(file);
|
|
//保存关联关系
|
|
//保存关联关系
|
|
CmProductArchiveFile archiveFile = new CmProductArchiveFile();
|
|
CmProductArchiveFile archiveFile = new CmProductArchiveFile();
|
|
archiveFile.setFileName(fileName);
|
|
archiveFile.setFileName(fileName);
|
|
archiveFile.setOssName(filePath);
|
|
archiveFile.setOssName(filePath);
|
|
archiveFile.setOssUrl(url);
|
|
archiveFile.setOssUrl(url);
|
|
|
|
+// archiveFile.setHtmlUrl(htmlUrl);
|
|
archiveFile.setUploadTime(new Date());
|
|
archiveFile.setUploadTime(new Date());
|
|
cmProductArchiveContentDao.insertFile(archiveFile);
|
|
cmProductArchiveContentDao.insertFile(archiveFile);
|
|
map.put("success", true);
|
|
map.put("success", true);
|
|
@@ -142,6 +187,102 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
|
|
return map;
|
|
return map;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private String generateHtml(File file, String fileShortName, String fileType) throws Exception {
|
|
|
|
+// String filePath = "/mnt/newdatadrive/data/runtime/tomcat-instance/manager/temp/tempHtml/";
|
|
|
|
+// String filePath = "D:\\poi-test\\wordToHtml\\";
|
|
|
|
+ if (fileType.contains("doc")) {
|
|
|
|
+ InputStream input = new FileInputStream(file);
|
|
|
|
+ HWPFDocument wordDocument = new HWPFDocument(input);
|
|
|
|
+ WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
|
|
|
|
+ DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
|
|
|
+ .newDocument());
|
|
|
|
+ wordToHtmlConverter.setPicturesManager((content, pictureType, suggestedName, widthInches, heightInches) -> suggestedName);
|
|
|
|
+ wordToHtmlConverter.processDocument(wordDocument);
|
|
|
|
+ List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
|
|
|
|
+ for (Picture pic : pics) {
|
|
|
|
+ try {
|
|
|
|
+ pic.writeImageContent(new FileOutputStream(htmlTempPath
|
|
|
|
+ + pic.suggestFullFileName()));
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Document htmlDocument = wordToHtmlConverter.getDocument();
|
|
|
|
+ ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
|
|
|
+ DOMSource domSource = new DOMSource(htmlDocument);
|
|
|
|
+ StreamResult streamResult = new StreamResult(outStream);
|
|
|
|
+ TransformerFactory tf = TransformerFactory.newInstance();
|
|
|
|
+ Transformer serializer = tf.newTransformer();
|
|
|
|
+ serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
|
|
|
|
+ serializer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
|
|
+ serializer.setOutputProperty(OutputKeys.METHOD, "html");
|
|
|
|
+ serializer.transform(domSource, streamResult);
|
|
|
|
+ outStream.close();
|
|
|
|
+ String content = new String(outStream.toByteArray());
|
|
|
|
+ FileUtils.writeStringToFile(new File(htmlTempPath, fileShortName + ".html"), content, "utf-8");
|
|
|
|
+ return htmlTempPath + fileShortName + ".html";
|
|
|
|
+ } else {
|
|
|
|
+ boolean isppt = checkFile(file);
|
|
|
|
+ if (!isppt) {
|
|
|
|
+ System.out.println("The image you specify don't exit!");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ FileInputStream is = new FileInputStream(file);
|
|
|
|
+ SlideShow ppt = new SlideShow(is);
|
|
|
|
+ is.close();
|
|
|
|
+ Dimension pgsize = ppt.getPageSize();
|
|
|
|
+ org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();
|
|
|
|
+ for (int i = 0; i < slide.length; i++) {
|
|
|
|
+ System.out.print("第" + i + "页。");
|
|
|
|
+ TextRun[] truns = slide[i].getTextRuns();
|
|
|
|
+ for ( int k=0;k<truns.length;k++){
|
|
|
|
+ RichTextRun[] rtruns = truns[k].getRichTextRuns();
|
|
|
|
+ for(int l=0;l<rtruns.length;l++){
|
|
|
|
+ int index = rtruns[l].getFontIndex();
|
|
|
|
+ String name = rtruns[l].getFontName();
|
|
|
|
+ rtruns[l].setFontIndex(1);
|
|
|
|
+ rtruns[l].setFontName("宋体");
|
|
|
|
+// System.out.println(rtruns[l].getText());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
+ Graphics2D graphics = img.createGraphics();
|
|
|
|
+ graphics.setPaint(Color.BLUE);
|
|
|
|
+ graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
|
|
|
|
+ slide[i].draw(graphics);
|
|
|
|
+ // 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径
|
|
|
|
+ FileOutputStream out = new FileOutputStream("D:/poi-test/pptToImg/pict_"+ (i + 1) + ".jpeg");
|
|
|
|
+ javax.imageio.ImageIO.write(img, "jpeg", out);
|
|
|
|
+ out.close();
|
|
|
|
+ }
|
|
|
|
+ System.out.println("success!!");
|
|
|
|
+ return null;
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
+ System.out.println(e);
|
|
|
|
+ // System.out.println("Can't find the image!");
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ // function 检查文件是否为PPT
|
|
|
|
+ public static boolean checkFile(File file) {
|
|
|
|
+ boolean isppt = false;
|
|
|
|
+ String filename = file.getName();
|
|
|
|
+ String suffixname = null;
|
|
|
|
+ if (filename != null && filename.indexOf(".") != -1) {
|
|
|
|
+ suffixname = filename.substring(filename.indexOf("."));
|
|
|
|
+ if (suffixname.equals(".ppt")) {
|
|
|
|
+ isppt = true;
|
|
|
|
+ }
|
|
|
|
+ return isppt;
|
|
|
|
+ } else {
|
|
|
|
+ return isppt;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/*@Transactional(readOnly = false)
|
|
/*@Transactional(readOnly = false)
|
|
public void deleteFile(Integer fileId) {
|
|
public void deleteFile(Integer fileId) {
|
|
CmOrderArchiveFile archiveFile = cmOrderArchiveDao.getArchiveFileById(fileId);
|
|
CmOrderArchiveFile archiveFile = cmOrderArchiveDao.getArchiveFileById(fileId);
|