오류수정

This commit is contained in:
leejunho 2024-09-25 18:15:40 +09:00
parent bd6d9a1cda
commit 26aba5ea90
7 changed files with 0 additions and 1310 deletions

View File

@ -1,162 +0,0 @@
package seed.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class SeedCvtDOCToHtml {
private Logger log = Logger.getLogger(this.getClass());
// public static void main(String args[]){
//
// String filePath = "F:/javaConvert/test/test.doc";
// String output = "F:/javaConvert/test/img/";
//
// new SeedCvtDOCToHtml().convert(filePath, output);
// }
public void convert(String filePath, String output) {
boolean success = false;
if(filePath.endsWith(".doc")) {
File file = new File(filePath);
if (file.exists()) {
POIFSFileSystem fs = null;
FileOutputStream fileOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
try {
fs = new POIFSFileSystem(new FileInputStream(filePath));
HWPFDocument hwpfDocument = new HWPFDocument(fs);
WordToHtmlConverter wordToHtmlConverter = new img(
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.processDocument(hwpfDocument);
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.transform(
new DOMSource(wordToHtmlConverter.getDocument()),
new StreamResult(stringWriter));
fileOutputStream = new FileOutputStream(output + getFileName(filePath) + ".jsp");
outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write("<%@ page contentType=\"text/html;charset=utf-8\" %>");
bufferedWriter.write("\n");
bufferedWriter.write(stringWriter.toString());
bufferedWriter.flush();
success = true;
} catch (TransformerConfigurationException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (TransformerFactoryConfigurationError e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (TransformerException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (ParserConfigurationException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (FactoryConfigurationError e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (FileNotFoundException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (UnsupportedEncodingException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (IllegalArgumentException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (IOException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (Exception e) {
log.error("CHECK ERROR:",e);
success = false;
} finally{
if(bufferedWriter!=null){try{bufferedWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(outputStreamWriter!=null){try{outputStreamWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(fileOutputStream!=null){try{fileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
}
}
if(!success){
SeedUtils.setSeedDeleteFiles(output, "");
}
}
}
public class img extends WordToHtmlConverter {
public img(Document document) {
super(document);
}
@Override
protected void processImageWithoutPicturesManager(Element currentBlock,
boolean inlined, Picture picture){
Element imgNode = currentBlock.getOwnerDocument().createElement("img");
StringBuilder sb = new StringBuilder();
sb.append(Base64.encodeBase64String(picture.getRawContent()));
sb.insert(0, "data:"+picture.getMimeType()+";base64,");
imgNode.setAttribute("src", sb.toString());
currentBlock.appendChild(imgNode);
}
}
private static String getFileName(String filePath){
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\");
return filePath.substring( sep + 1 , filePath.lastIndexOf("."));
}
}

View File

@ -1,137 +0,0 @@
package seed.utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import org.apache.log4j.Logger;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.core.XWPFConverterException;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class SeedCvtDOCXToHtml {
private Logger log = Logger.getLogger(this.getClass());
// public static void main(String args[]) {
//
// String rootPath = "F:/javaConvert";
// String filePath = "test/test.docx";
// String output = "F:/javaConvert/test/img/";
//
// new SeedCvtDOCXToHtml().convert(rootPath, filePath, output);
// }
public synchronized void convert(String rootPath, String filePath, String output) {
boolean success = false;
if(filePath.endsWith(".docx")) {
File file = new File(rootPath + "/" + filePath);
if (file.exists()) {
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
FileOutputStream fileOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
try{
StringBuffer fileHtml = new StringBuffer();
InputStream in = new FileInputStream(file);
XWPFDocument document = new XWPFDocument(in);
XHTMLOptions options = XHTMLOptions.create();
File imageFolder = new File(output + getFileName(filePath) + "/img");
options.setExtractor( new FileImageExtractor(imageFolder));
options.URIResolver( new FileURIResolver(imageFolder));
fileOutputStream = new FileOutputStream(new File(output + getFileName(filePath) + "_org_.jsp"));
XHTMLConverter.getInstance().convert(document, fileOutputStream, options);
//이미지 경로때문에 파일 읽어서 다시 쓰기 해야함(서버경로에서 해당 파일 경로로 변경)
File tmpFile = new File(output + getFileName(filePath) + "_org_.jsp");
if(tmpFile.exists()){
fileInputStream = new FileInputStream(output + getFileName(filePath) + "_org_.jsp");
inputStreamReader = new InputStreamReader(fileInputStream, "EUC-KR");
bufferedReader = new BufferedReader(inputStreamReader);
String fileBuffer = "";
while((fileBuffer = bufferedReader.readLine()) != null){
fileHtml.append(fileBuffer);
fileHtml.append("\n");
}
fileOutputStream = new FileOutputStream(output + getFileName(filePath) + ".jsp");
outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write("<%@ page contentType=\"text/html;charset=utf-8\" %>");
bufferedWriter.write("\n");
bufferedWriter.write(fileHtml.toString().replaceAll("\\\\", "/").replaceAll(output, "/site/"+filePath.substring(0, filePath.lastIndexOf("/"))+"/"+getFileName(filePath)+"/convert/"));
bufferedWriter.flush();
success = true;
}
} catch (FileNotFoundException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (UnsupportedEncodingException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (XWPFConverterException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (IOException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (Exception e) {
log.error("CHECK ERROR:",e);
success = false;
} finally{
if(bufferedReader!=null){try{bufferedReader.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(inputStreamReader!=null){try{inputStreamReader.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(fileInputStream!=null){try{fileInputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(bufferedWriter!=null){try{bufferedWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(outputStreamWriter!=null){try{outputStreamWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(fileOutputStream!=null){try{fileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
}
}
if(!success){
SeedUtils.setSeedDeleteFiles(output, "");
}else{
SeedUtils.setSeedDeleteFile(output + getFileName(filePath) + "_org_.jsp");
}
}
}
private static String getFileName(String filePath){
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\");
return filePath.substring( sep + 1 , filePath.lastIndexOf("."));
}
}

View File

@ -1,148 +0,0 @@
package seed.utils;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class SeedCvtPDFToImg {
private Logger log = Logger.getLogger(this.getClass());
// public static void main(String[] args) {
//
// String rootPath = "F:/javaConvert";
// String filePath = "test/test.pdf";
// String output = "F:/javaConvert/test/img/";
//
// new SeedCvtPDFToImg().convert(rootPath, filePath, output);
// }
@SuppressWarnings("unchecked")
public void convert(String rootPath, String filePath, String output) {
boolean success = false;
if(filePath.endsWith(".pdf")) {
File file = new File(rootPath + "/" + filePath);
if (file.exists()) {
FileOutputStream fileOutputStream = null;
FileOutputStream htmlFileOutputStream = null;
OutputStreamWriter htmlOutputStreamWriter = null;
BufferedWriter htmlWriter = null;
try {
PDDocument document = PDDocument.load(rootPath + "/" + filePath);
List<PDPage> list = document.getDocumentCatalog().getAllPages();
if(list.size() > 0){
SeedUtils.setSeedMkDirs(output);
}
//html파일 작성
htmlFileOutputStream = new FileOutputStream(output + "/" + getFileName(filePath) + ".jsp");
htmlOutputStreamWriter = new OutputStreamWriter(htmlFileOutputStream, "UTF-8");
htmlWriter = new BufferedWriter(htmlOutputStreamWriter);
htmlWriter.write("<%@ page contentType=\"text/html;charset=utf-8\" %>");
htmlWriter.write("\n");
htmlWriter.write("<!doctype html>");
htmlWriter.write("\n");
htmlWriter.write("<!--[if lt IE 7 ]><html class=\"ie ie6\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if IE 7 ]><html class=\"ie ie7\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if IE 8 ]><html class=\"ie ie8\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if (gte IE 9)|!(IE)]><!--><html lang=\"ko\"><!--<![endif]--> ");
htmlWriter.write("\n");
htmlWriter.write(" <head>");
htmlWriter.write("\n");
htmlWriter.write(" <meta charset=\"utf-8\">");
htmlWriter.write("\n");
htmlWriter.write(" <meta name=\"robots\" content=\"noindex\">");
htmlWriter.write("\n");
htmlWriter.write(" <!--[if IE]>");
htmlWriter.write("\n");
htmlWriter.write(" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">");
htmlWriter.write("\n");
htmlWriter.write(" <![endif]-->");
htmlWriter.write("\n");
htmlWriter.write(" <title>미리보기 페이지</title>");
htmlWriter.write("\n");
htmlWriter.write(" <!-- js -->");
htmlWriter.write("\n");
htmlWriter.write(" <!--[if lte IE 9]>");
htmlWriter.write("\n");
htmlWriter.write(" <script src=\"/js/html5.js\"></script>");
htmlWriter.write("\n");
htmlWriter.write(" <![endif]-->");
htmlWriter.write("\n");
htmlWriter.write(" </head>");
htmlWriter.write("\n");
htmlWriter.write(" <body>");
htmlWriter.write("\n");
for(int i=0; i<list.size(); i++){
PDPage page = list.get(i);
BufferedImage img = page.convertToImage();
fileOutputStream = new FileOutputStream(output + getFileName(filePath) + "_" + (i + 1) + ".jpg");
ImageIO.write(img, "jpg", fileOutputStream);
htmlWriter.write(" <img src='/site/"+filePath.substring(0, filePath.lastIndexOf("/"))+"/"+getFileName(filePath)+"/convert/" + getFileName(filePath) + "_" + (i + 1) + ".jpg' alt='" + getFileName(filePath) + "_" + (i + 1) + "' />");
htmlWriter.write("\n");
}
htmlWriter.write(" </body>");
htmlWriter.write("\n");
htmlWriter.write("</html>");
htmlWriter.write("\n");
htmlWriter.flush();
document.close();
success = true;
} catch (FileNotFoundException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (IOException e) {
log.error("CHECK ERROR:",e);
success = false;
}finally{
if(fileOutputStream!=null){try{fileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlWriter!=null){try{htmlWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlOutputStreamWriter!=null){try{htmlOutputStreamWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlFileOutputStream!=null){try{htmlFileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
}
}
if(!success){
SeedUtils.setSeedDeleteFiles(output, "");
}
}
}
private static String getFileName(String filePath){
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\");
return filePath.substring( sep + 1 , filePath.lastIndexOf("."));
}
}

View File

@ -1,164 +0,0 @@
package seed.utils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public class SeedCvtPPTToImg {
private Logger log = Logger.getLogger(this.getClass());
// public static void main(String[] args) {
//
// String rootPath = "F:/javaConvert";
// String filePath = "test/test.ppt";
// String output = "F:/javaConvert/test/img/";
//
// new SeedCvtPPTToImg().convert(rootPath, filePath, output);
// }
public void convert(String rootPath, String filePath, String output) {
boolean success = false;
if(filePath.endsWith(".ppt")) {
File file = new File(rootPath + "/" + filePath);
if (file.exists()) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
FileOutputStream htmlFileOutputStream = null;
OutputStreamWriter htmlOutputStreamWriter = null;
BufferedWriter htmlWriter = null;
try {
fileInputStream = new FileInputStream(file);
SlideShow ppt = new SlideShow(fileInputStream);
double zoom = 2; // magnify it by 2
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
if(slide.length > 0){
SeedUtils.setSeedMkDirs(output);
}
//html?Œ???‘??
htmlFileOutputStream = new FileOutputStream(output + "/" + getFileName(filePath) + ".jsp");
htmlOutputStreamWriter = new OutputStreamWriter(htmlFileOutputStream, "UTF-8");
htmlWriter = new BufferedWriter(htmlOutputStreamWriter);
htmlWriter.write("<%@ page contentType=\"text/html;charset=utf-8\" %>");
htmlWriter.write("\n");
htmlWriter.write("<!doctype html>");
htmlWriter.write("\n");
htmlWriter.write("<!--[if lt IE 7 ]><html class=\"ie ie6\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if IE 7 ]><html class=\"ie ie7\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if IE 8 ]><html class=\"ie ie8\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if (gte IE 9)|!(IE)]><!--><html lang=\"ko\"><!--<![endif]--> ");
htmlWriter.write("\n");
htmlWriter.write(" <head>");
htmlWriter.write("\n");
htmlWriter.write(" <meta charset=\"utf-8\">");
htmlWriter.write("\n");
htmlWriter.write(" <meta name=\"robots\" content=\"noindex\">");
htmlWriter.write("\n");
htmlWriter.write(" <!--[if IE]>");
htmlWriter.write("\n");
htmlWriter.write(" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">");
htmlWriter.write("\n");
htmlWriter.write(" <![endif]-->");
htmlWriter.write("\n");
htmlWriter.write(" <title>誘몃━蹂닿린 ?˜?댁?</title>");
htmlWriter.write("\n");
htmlWriter.write(" <!-- js -->");
htmlWriter.write("\n");
htmlWriter.write(" <!--[if lte IE 9]>");
htmlWriter.write("\n");
htmlWriter.write(" <script src=\"/js/html5.js\"></script>");
htmlWriter.write("\n");
htmlWriter.write(" <![endif]-->");
htmlWriter.write("\n");
htmlWriter.write(" </head>");
htmlWriter.write("\n");
htmlWriter.write(" <body>");
htmlWriter.write("\n");
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
fileOutputStream = new FileOutputStream(output + getFileName(filePath) + "_" + (i + 1) + ".jpg");
ImageIO.write(img, "jpg", fileOutputStream);
htmlWriter.write(" <img src='/site/"+filePath.substring(0, filePath.lastIndexOf("/"))+"/"+getFileName(filePath)+"/convert/" + getFileName(filePath) + "_" + (i + 1) + ".jpg' alt='" + getFileName(filePath) + "_" + (i + 1) + "' />");
htmlWriter.write("\n");
}
htmlWriter.write(" </body>");
htmlWriter.write("\n");
htmlWriter.write("</html>");
htmlWriter.write("\n");
htmlWriter.flush();
success = true;
} catch (FileNotFoundException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (IOException e) {
log.error("CHECK ERROR:",e);
success = false;
}finally{
if(fileInputStream!=null){try{fileInputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(fileOutputStream!=null){try{fileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlWriter!=null){try{htmlWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlOutputStreamWriter!=null){try{htmlOutputStreamWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlFileOutputStream!=null){try{htmlFileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
}
}
if(!success){
SeedUtils.setSeedDeleteFiles(output, "");
}
}
}
private static String getFileName(String filePath){
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\");
return filePath.substring( sep + 1 , filePath.lastIndexOf("."));
}
}

View File

@ -1,164 +0,0 @@
package seed.utils;
import java.awt.Dimension;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class SeedCvtPPTXToImg {
private Logger log = Logger.getLogger(this.getClass());
// public static void main(String[] args) {
//
// String rootPath = "F:/javaConvert";
// String filePath = "test/test.pptx";
// String output = "F:/javaConvert/test/img/";
//
// new SeedCvtPPTXToImg().convert(rootPath, filePath, output);
// }
public void convert(String rootPath, String filePath, String output) {
boolean success = false;
if(filePath.endsWith(".pptx")) {
File file = new File(rootPath + "/" + filePath);
if (file.exists()) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
FileOutputStream htmlFileOutputStream = null;
OutputStreamWriter htmlOutputStreamWriter = null;
BufferedWriter htmlWriter = null;
try {
fileInputStream = new FileInputStream(file);
XMLSlideShow ppt = new XMLSlideShow(fileInputStream);
double zoom = 2; // magnify it by 2
AffineTransform at = new AffineTransform();
at.setToScale(zoom, zoom);
Dimension pgsize = ppt.getPageSize();
XSLFSlide[] slide = ppt.getSlides();
if(slide.length > 0){
SeedUtils.setSeedMkDirs(output);
}
//html?Œ???‘??
htmlFileOutputStream = new FileOutputStream(output + "/" + getFileName(filePath) + ".jsp");
htmlOutputStreamWriter = new OutputStreamWriter(htmlFileOutputStream, "UTF-8");
htmlWriter = new BufferedWriter(htmlOutputStreamWriter);
htmlWriter.write("<%@ page contentType=\"text/html;charset=utf-8\" %>");
htmlWriter.write("\n");
htmlWriter.write("<!doctype html>");
htmlWriter.write("\n");
htmlWriter.write("<!--[if lt IE 7 ]><html class=\"ie ie6\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if IE 7 ]><html class=\"ie ie7\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if IE 8 ]><html class=\"ie ie8\" lang=\"ko\"><![endif]-->");
htmlWriter.write("\n");
htmlWriter.write("<!--[if (gte IE 9)|!(IE)]><!--><html lang=\"ko\"><!--<![endif]--> ");
htmlWriter.write("\n");
htmlWriter.write(" <head>");
htmlWriter.write("\n");
htmlWriter.write(" <meta charset=\"utf-8\">");
htmlWriter.write("\n");
htmlWriter.write(" <meta name=\"robots\" content=\"noindex\">");
htmlWriter.write("\n");
htmlWriter.write(" <!--[if IE]>");
htmlWriter.write("\n");
htmlWriter.write(" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">");
htmlWriter.write("\n");
htmlWriter.write(" <![endif]-->");
htmlWriter.write("\n");
htmlWriter.write(" <title>誘몃━蹂닿린 ?˜?댁?</title>");
htmlWriter.write("\n");
htmlWriter.write(" <!-- js -->");
htmlWriter.write("\n");
htmlWriter.write(" <!--[if lte IE 9]>");
htmlWriter.write("\n");
htmlWriter.write(" <script src=\"/js/html5.js\"></script>");
htmlWriter.write("\n");
htmlWriter.write(" <![endif]-->");
htmlWriter.write("\n");
htmlWriter.write(" </head>");
htmlWriter.write("\n");
htmlWriter.write(" <body>");
htmlWriter.write("\n");
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage((int)Math.ceil(pgsize.width*zoom), (int)Math.ceil(pgsize.height*zoom), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setTransform(at);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slide[i].draw(graphics);
fileOutputStream = new FileOutputStream(output + getFileName(filePath) + "_" + (i + 1) + ".jpg");
ImageIO.write(img, "jpg", fileOutputStream);
htmlWriter.write(" <img src='/site/"+filePath.substring(0, filePath.lastIndexOf("/"))+"/"+getFileName(filePath)+"/convert/" + getFileName(filePath) + "_" + (i + 1) + ".jpg' alt='" + getFileName(filePath) + "_" + (i + 1) + "' />");
htmlWriter.write("\n");
}
htmlWriter.write(" </body>");
htmlWriter.write("\n");
htmlWriter.write("</html>");
htmlWriter.write("\n");
htmlWriter.flush();
success = true;
} catch (FileNotFoundException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (IOException e) {
log.error("CHECK ERROR:",e);
success = false;
}finally{
if(fileInputStream!=null){try{fileInputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(fileOutputStream!=null){try{fileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlWriter!=null){try{htmlWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlOutputStreamWriter!=null){try{htmlOutputStreamWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(htmlFileOutputStream!=null){try{htmlFileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
}
}
if(!success){
SeedUtils.setSeedDeleteFiles(output, "");
}
}
}
private static String getFileName(String filePath){
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\");
return filePath.substring( sep + 1 , filePath.lastIndexOf("."));
}
}

View File

@ -1,139 +0,0 @@
package seed.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.converter.ExcelToHtmlConverter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class SeedCvtXLSToHtml {
private Logger log = Logger.getLogger(this.getClass());
// public static void main(String args[]){
//
// String filePath = "F:/javaConvert/test/test.xls";
// String output = "F:/javaConvert/test/img/";
//
// new SeedCvtXLSToHtml().convert(filePath, output);
// }
public void convert(String filePath, String output) {
boolean success = false;
if(filePath.endsWith(".xls")) {
File file = new File(filePath);
if (file.exists()) {
POIFSFileSystem fs = null;
FileOutputStream fileOutputStream = null;
OutputStreamWriter outputStreamWriter = null;
BufferedWriter bufferedWriter = null;
try {
fs = new POIFSFileSystem(new FileInputStream(filePath));
HSSFWorkbook hSSFWorkbook = new HSSFWorkbook(fs);
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
excelToHtmlConverter.processWorkbook(hSSFWorkbook);
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.transform(
new DOMSource(excelToHtmlConverter.getDocument()),
new StreamResult(stringWriter));
fileOutputStream = new FileOutputStream(output + getFileName(filePath) + ".jsp");
outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write("<%@ page contentType=\"text/html;charset=utf-8\" %>");
bufferedWriter.write("\n");
bufferedWriter.write(stringWriter.toString());
bufferedWriter.flush();
success = true;
} catch (TransformerConfigurationException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (TransformerFactoryConfigurationError e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (TransformerException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (ParserConfigurationException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (FactoryConfigurationError e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (FileNotFoundException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (UnsupportedEncodingException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (IllegalArgumentException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (IOException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (Exception e) {
log.error("CHECK ERROR:",e);
success = false;
} finally{
if(bufferedWriter!=null){try{bufferedWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(outputStreamWriter!=null){try{outputStreamWriter.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
if(fileOutputStream!=null){try{fileOutputStream.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
}
}
if(!success){
SeedUtils.setSeedDeleteFiles(output, "");
}
}
}
private static String getFileName(String filePath){
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\");
return filePath.substring( sep + 1 , filePath.lastIndexOf("."));
}
}

View File

@ -1,396 +0,0 @@
package seed.utils;
import com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.log4j.Logger;
import org.apache.poi.hwpf.converter.HtmlDocumentFacade;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class SeedCvtXLSXToHtml {
private Logger log = Logger.getLogger(this.getClass());
// public static void main(String[] args) {
//
// String filePath = "F:/javaConvert/test/test.xlsx";
// String output = "F:/javaConvert/test/img/";
//
// new SeedCvtXLSXToHtml().convert(filePath, output);
// }
public void convert(String filePath, String output) {
boolean success = false;
if(filePath.endsWith(".xlsx")) {
File file = new File(filePath);
if (file.exists()) {
StringBuilder css = new StringBuilder();
try{
OPCPackage op = OPCPackage.open(filePath);
XSSFWorkbook x = new XSSFWorkbook(op);
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
HtmlDocumentFacade htmlDocumentFacade = new HtmlDocumentFacade(document);
Element window = htmlDocumentFacade.createBlock();
window.setAttribute("id", "window");
Element page = htmlDocumentFacade.createBlock();
page.setAttribute("id", "page");
window.appendChild(page);
htmlDocumentFacade.getBody().appendChild(window);
Integer sheetNum = x.getNumberOfSheets();
for (int i = 0; i < sheetNum; i++) {
XSSFSheet sheet = x.getSheet(x.getSheetName(i));
String sheetName = x.getSheetName(i);
{
Element title = htmlDocumentFacade.createHeader2();
title.setTextContent(sheetName);
page.appendChild(title);
}
processSheet(htmlDocumentFacade, page, sheet, "_" + i + "_");
}
htmlDocumentFacade.updateStylesheet();
Element style = (Element) htmlDocumentFacade.getDocument().getElementsByTagName("style").item(0);
style.setTextContent(css.append(style.getTextContent()).toString());
saveAsHtml(output + getFileName(filePath) + ".jsp", htmlDocumentFacade.getDocument());
success = true;
} catch (IOException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (InvalidFormatException e) {
log.error("CHECK ERROR:",e);
success = false;
} catch (ParserConfigurationException e) {
log.error("CHECK ERROR:",e);
success = false;
}
}
if(!success){
SeedUtils.setSeedDeleteFiles(output, "");
}
}
}
private static String getFileName(String filePath){
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\");
return filePath.substring( sep + 1 , filePath.lastIndexOf("."));
}
private void processSheet(HtmlDocumentFacade htmlDocumentFacade, Element container, XSSFSheet sheet, String sID) {
StringBuilder css = new StringBuilder();
Element table = htmlDocumentFacade.createTable();
int sIndex = sheet.getWorkbook().getSheetIndex(sheet);
String sId = "sheet_".concat(String.valueOf(sIndex));
table.setAttribute("id", sId);
table.setAttribute("border", "1");
table.setAttribute("cellpadding", "2");
table.setAttribute("cellspacing", "0");
table.setAttribute("style", "border-collapse: collapse;");
if (sheet.getDefaultRowHeightInPoints() > 0) {
css.append("#").append(sId).append(" tr{height:").append(sheet.getDefaultRowHeightInPoints() / 28.34).append("cm}\n");
}
if (sheet.getDefaultColumnWidth() > 0) {
css.append("#").append(sId).append(" td{width:").append(sheet.getDefaultColumnWidth() * 0.21).append("cm}\n");
}
generateColumns(htmlDocumentFacade, sheet, table);
final short col_num = get_col_max(sheet);
final int row_num = sheet.getLastRowNum() + 1;
for (int i = 0; i < row_num; i++) {
Row row = sheet.getRow(i);
processRow(htmlDocumentFacade, table, (XSSFRow) row, sheet, col_num, sID, i);
}
container.appendChild(table);
}
private short get_col_max(XSSFSheet sheet) {
short ans = -1;
//rows
Iterator<Row> rows = sheet.iterator();
while (rows.hasNext()) {
Row row = rows.next();
if (row instanceof XSSFRow) {
short c = (short) (row.getLastCellNum());
if (ans < c) {
ans = c;
}
}
}
return ans;
}
@SuppressWarnings("deprecation")
private void generateColumns(HtmlDocumentFacade htmlDocumentFacade, XSSFSheet sheet, Element table) {
List<CTCols> colsList = sheet.getCTWorksheet().getColsList();
MathContext mc = new MathContext(3);
for (CTCols cols : colsList) {
long oldLevel = 1;
for (CTCol col : cols.getColArray()) {
while (true) {
if (oldLevel == col.getMin()) {
break;
}
Element column = htmlDocumentFacade.createTableColumn();
// htmlDocumentFacade.addStyleClass(column, "col", "width:2cm;");
column.setAttribute("style", "width:2cm;");
table.appendChild(column);
oldLevel++;
}
Element column = htmlDocumentFacade.createTableColumn();
String width = new BigDecimal(sheet.getColumnWidth(Long.bitCount(oldLevel)) / 1440.0, mc).toString();
column.setAttribute("style", "width:".concat(width).concat("cm;"));
table.appendChild(column);
oldLevel++;
}
}
}
private void processRow(HtmlDocumentFacade htmlDocumentFacade, Element table, XSSFRow row, XSSFSheet sheet, final int col_num, String sID, int pos_row) {
Element tr = htmlDocumentFacade.createTableRow();
if (!(row instanceof XSSFRow)) {
for (int pos_col = 0; pos_col < col_num; pos_col++) {
processCell(htmlDocumentFacade, tr, null, sID, pos_col, pos_row); // empty line
}
} else {
//if (row.isFormatted()) {}
if (row.getCTRow().getCustomHeight()) {
tr.setAttribute("style", "height:".concat(String.valueOf(row.getHeightInPoints())).concat("pt;"));
}
for (int pos_col = 0; pos_col < col_num; pos_col++) {
Cell cell = row.getCell(pos_col);
if (cell instanceof XSSFCell) {
processCell(htmlDocumentFacade, tr, (XSSFCell) cell, sID, pos_col, pos_row);
} else {
processCell(htmlDocumentFacade, tr, null, sID, pos_col, pos_row);
}
}
}
table.appendChild(tr);
}
private void processCell(HtmlDocumentFacade htmlDocumentFacade, Element tr, XSSFCell cell, String sID, int pos_col, int pos_row) {
int cols = 1;
int rows = 1;
if (cell != null) {
if (cell != null) {
int num = cell.getSheet().getNumMergedRegions();
for (int i = 0; i < num; i++) {
CellRangeAddress c = cell.getSheet().getMergedRegion(i);
int x0 = c.getFirstColumn();
int x1 = c.getLastColumn();
int y0 = c.getFirstRow();
int y1 = c.getLastRow();
if (x0 == pos_col && y0 == pos_row) {
cols = c.getLastColumn() - c.getFirstColumn() + 1;
rows = c.getLastRow() - c.getFirstRow() + 1;
} else if ((x0 <= pos_col) && (pos_col <= x1) && (y0 <= pos_row) && (pos_row <= y1)) {
return;
}
}
}
}
Element td = htmlDocumentFacade.createTableCell();
if (cols > 1) {
td.setAttribute("colspan", "" + cols);
}
if (rows > 1) {
td.setAttribute("rowspan", "" + rows);
}
Object value;
if (cell == null) {
td.setTextContent("\u00a0");
} else {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
value = "\u00a0";
break;
case Cell.CELL_TYPE_NUMERIC:
value = cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
value = cell.getNumericCellValue();
break;
default:
value = cell.getRichStringCellValue();
break;
}
if (value instanceof XSSFRichTextString) {
processCellStyle(htmlDocumentFacade, td, cell.getCellStyle(), (XSSFRichTextString) value, sID);
td.setTextContent(SeedUtils.setReplaceNull(value));
} else {
processCellStyle(htmlDocumentFacade, td, cell.getCellStyle(), null, sID);
td.setTextContent(SeedUtils.setReplaceNull(value));
}
}
tr.appendChild(td);
}
private void processCellStyle(HtmlDocumentFacade htmlDocumentFacade, Element td, XSSFCellStyle style, XSSFRichTextString rts, String sID) {
StringBuilder sb = new StringBuilder();
if (rts != null) {
XSSFFont font = rts.getFontOfFormattingRun(1);
if (font != null) {
sb.append("font-family:").append(font.getFontName()).append(";");
sb.append("font-size:").append(font.getFontHeightInPoints()).append("pt;");
if (font.getXSSFColor() != null) {
String color = font.getXSSFColor().getARGBHex().substring(2);
sb.append("color:#").append(color).append(";");
}
if (font.getItalic()) {
sb.append("font-style:italic;");
}
if (font.getBold()) {
sb.append("font-weight:").append(font.getBoldweight()).append(";");
}
if (font.getStrikeout()) {
sb.append("text-decoration:underline;");
}
}
}
if (style.getAlignment() != 1) {
switch (style.getAlignment()) {
case 2:
sb.append("text-align:").append("center;");
break;
case 3:
sb.append("text-align:").append("right;");
break;
}
}
if (style.getBorderBottom() != 0) {
sb.append("border-bottom:solid; ").append(style.getBorderBottom()).append("px;");
}
if (style.getBorderLeft() != 0) {
sb.append("border-left:solid; ").append(style.getBorderLeft()).append("px;");
}
if (style.getBorderTop() != 0) {
sb.append("border-top:solid; ").append(style.getBorderTop()).append("px;");
}
if (style.getBorderRight() != 0) {
sb.append("border-right:solid; ").append(style.getBorderRight()).append("px;");
}
if (style.getFillBackgroundXSSFColor() != null) {
sb.append("background:#fcc;");
}
htmlDocumentFacade.addStyleClass(td, "td" + sID, sb.toString());
}
private void saveAsHtml(String output, org.w3c.dom.Document document) {
FileOutputStream fos = null;
try{
File folder = new File(getFilePath(output));
if (!folder.canRead()) {
folder.mkdirs();
}
fos = new FileOutputStream(output);
DOMSource domSource = new DOMSource(document);
StreamResult result = new StreamResult(new StringWriter());
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
serializer.transform(domSource, result);
String s = "<!doctype html>\n" + result.getWriter().toString();
OutputStreamWriter out = new OutputStreamWriter(fos, "UTF-8");
out.write("<%@ page contentType=\"text/html;charset=utf-8\" %>");
out.write("\n");
out.write(s);
out.close();
} catch (FileNotFoundException e) {
log.error("CHECK ERROR:",e);
} catch (TransformerException e) {
log.error("CHECK ERROR:",e);
} catch (IllegalArgumentException e) {
log.error("CHECK ERROR:",e);
} catch (IOException e) {
log.error("CHECK ERROR:",e);
} catch (Exception e) {
log.error("CHECK ERROR:",e);
} finally{
if(fos!=null){try{fos.close();}catch(IOException e){log.error("CHECK ERROR:",e);}}
}
}
public String getFilePath(String fileFullPath) {
int sep = fileFullPath.lastIndexOf("\\") == -1 ? fileFullPath.lastIndexOf("/") : fileFullPath.lastIndexOf("\\");
return fileFullPath.substring(0, sep + 1);
}
}