Aslee 3 роки тому
батько
коміт
7de986b4a1

+ 147 - 6
src/main/java/com/caimei/modules/archive/service/CmProductArchiveContentService.java

@@ -1,7 +1,13 @@
 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.List;
 
 import com.caimei.dfs.image.beens.ImageUploadInfo;
 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.utils.Encodes;
 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.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.dao.CmProductArchiveContentDao;
 import org.springframework.web.multipart.MultipartFile;
+import org.w3c.dom.Document;
 
 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
@@ -37,7 +60,9 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
 	public CmProductArchiveContent get(String id) {
 		return super.get(id);
 	}
-	
+
+	private final String htmlTempPath = Global.getConfig("archive.htmlTempPath");
+
 	public List<CmProductArchiveContent> findList(CmProductArchiveContent cmProductArchiveContent) {
 		return super.findList(cmProductArchiveContent);
 	}
@@ -57,7 +82,15 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
 			imageList.forEach(image -> {
 				if (StringUtils.isNotEmpty(image.getOssUrl())) {
 					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()));
 					cmProductArchiveContentDao.insertFile(image);
@@ -112,22 +145,34 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
         Map<String, Object> map = new HashMap<>();
         String fileAllName = multipartFile.getOriginalFilename();
         String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1);
+		String fileShortName = fileAllName.substring(0, fileAllName.lastIndexOf("."));
         String uuid = UUID.randomUUID().toString().replaceAll("-", "");
         String filePath = uuid + "." + fileType;
         String contentType = OssArchiveUtil.getContentType(fileAllName);
         try {
             //保存本地
             File file = OssArchiveUtil.ossUpload(multipartFile);
-            logger.info("默认路径>>>" + file.getAbsolutePath());
+			logger.info("默认路径>>>" + file.getAbsolutePath());
             //上传oss
             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();
             archiveFile.setFileName(fileName);
             archiveFile.setOssName(filePath);
             archiveFile.setOssUrl(url);
+//			archiveFile.setHtmlUrl(htmlUrl);
             archiveFile.setUploadTime(new Date());
             cmProductArchiveContentDao.insertFile(archiveFile);
             map.put("success", true);
@@ -142,6 +187,102 @@ public class CmProductArchiveContentService extends CrudService<CmProductArchive
         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)
     public void deleteFile(Integer fileId) {
         CmOrderArchiveFile archiveFile = cmOrderArchiveDao.getArchiveFileById(fileId);

+ 1 - 1
src/main/webapp/WEB-INF/views/modules/archive/addFileForm.jsp

@@ -183,7 +183,7 @@
         <label class="control-label"><font color="red">*</font>文件路径:</label>
         <input id="uploadFileName" type="text" style="display: inline;margin-left:20px" disabled="true" class="input-xlarge required" />
         <div class="upload">
-            <input type="file" name="file" id="archiveFile" accept=".pdf,.doc,.docx,.ppt">选择文件
+            <input type="file" name="file" id="archiveFile" accept=".pdf,.doc,.docx,.ppt,.pptx">选择文件
         </div>
         <div class="add-submit">
             <input id="addSubmit" type="button" value="上传" style="cursor: pointer"/>上传&nbsp;

+ 2 - 2
src/main/webapp/WEB-INF/views/modules/archive/cmProductArchiveForm.jsp

@@ -165,8 +165,8 @@
 			<label class="control-label">商品分类:</label>
 			<div class="controls">
 				<form:select path="productClassify" class="input-medium ">
-					<form:option value="1" label="生美"/>
-					<form:option value="2" label="医美"/>
+					<form:option value="2" label="生美"/>
+					<form:option value="1" label="医美"/>
 				</form:select>
 			</div>
 		</div>

+ 3 - 3
src/main/webapp/WEB-INF/views/modules/archive/cmProductArchiveList.jsp

@@ -49,8 +49,8 @@
 			 <label>商品分类:</label>
 				<form:select path="productClassify" class="input-medium">
 					<form:option value="" label="全部"/>
-					<form:option value="1" label="生美"/>
-					<form:option value="2" label="医美"/>
+					<form:option value="2" label="生美"/>
+					<form:option value="1" label="医美"/>
 				</form:select>
 			&nbsp;&nbsp;<input id="btnSubmit" class="btn btn-primary" type="submit" value="查询"/>
 			&nbsp;&nbsp;<input class="btn btn-primary" style="width: 50px"
@@ -95,7 +95,7 @@
 					${cmProductArchive.productType eq 1?'产品':'仪器'}
 				</td>
 				<td>
-					${cmProductArchive.productClassify eq 1?'生美':'医美'}
+					${cmProductArchive.productClassify eq 1?'医美':'生美'}
 				</td>
 				<td>
 					<fmt:formatDate value="${cmProductArchive.addTime}" pattern="yyyy-MM-dd HH:mm:ss"/>