-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
4,355 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title></title> | ||
<link rel="stylesheet" | ||
href="static/bootstrap-3.3.5-dist/css/bootstrap.min.css"> | ||
|
||
</head> | ||
<body > | ||
<div class="container"> | ||
<div class="jumbotron"> | ||
<h2>MS Office 生成HTML,网页预览</h2> | ||
<p>访问端口8080</p> | ||
<form enctype="multipart/form-data" method="post" action="/convert" id="upload-form"> | ||
<div class="form-group"> | ||
<label for="file">上传文档</label> | ||
<input type="file" class="form-control" id="file" name="file" > | ||
</div> | ||
<button type="button" class="btn btn-primary" onclick="upload()">上传</button> | ||
</form> | ||
</div> | ||
</div> | ||
<div id="preview"></div> | ||
<script type="text/javascript" src="static/jquery.min.js"></script> | ||
<script type="text/javascript" | ||
src="static/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script> | ||
<script type="text/javascript"> | ||
function upload() { | ||
var formData = new FormData($( "#upload-form" )[0]); | ||
$.ajax({ | ||
url: 'poi?method=convert' , | ||
type: 'POST', | ||
data: formData, | ||
async: false, | ||
cache: false, | ||
contentType: false, | ||
processData: false, | ||
success: function (returndata) { | ||
|
||
console.log(JSON.parse(returndata).content); | ||
$("#preview").html(JSON.parse(returndata).content); | ||
}, | ||
error: function (returndata) { | ||
console.log("error:") | ||
} | ||
}); | ||
} | ||
|
||
</script> | ||
|
||
</body> | ||
</html> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cc.mask.utils; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
|
||
public final class HtmlUtil { | ||
|
||
|
||
private Element styleSheet = null; | ||
public Element window = null; | ||
public Element body = null; | ||
public Element script = null; | ||
public Element html = null; | ||
public Document document = null; | ||
|
||
|
||
public HtmlUtil(){ | ||
try { | ||
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); | ||
} catch (ParserConfigurationException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void createHTML(){ | ||
html = document.createElement("html"); | ||
|
||
Element head = document.createElement("head"); | ||
styleSheet = document.createElement("style"); | ||
styleSheet.setAttribute( "type", "text/css" ); | ||
script = document.createElement("script"); | ||
script.setAttribute("language", "text/javascript"); | ||
head.appendChild(script); | ||
head.appendChild(styleSheet); | ||
html.appendChild(head); | ||
|
||
body = document.createElement("body"); | ||
window = createBlock(); | ||
body.appendChild(window); | ||
html.appendChild(body); | ||
|
||
document.appendChild(html); | ||
|
||
} | ||
|
||
|
||
public void addClass(Element ele, String className, String style){ | ||
String oldClazz = ele.getAttribute( "class" ); | ||
|
||
|
||
|
||
ele.setAttribute("class", isEmpty(oldClazz) ? className : className +" " + oldClazz); | ||
|
||
} | ||
|
||
public Element createBlock(){ | ||
return document.createElement("div"); | ||
} | ||
|
||
public void createStyle(){ | ||
|
||
|
||
|
||
} | ||
|
||
public static boolean isEmpty(String str){ | ||
return str == null || str.trim().length() == 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cc.mask.utils; | ||
|
||
|
||
|
||
public class StringUtil { | ||
|
||
/** | ||
* Returns <code>true</code> if the provided String is either <code>null</code> | ||
* or the empty String <code>""</code>.<p> | ||
* | ||
* @param value the value to check | ||
* | ||
* @return true, if the provided value is null or the empty String, false otherwise | ||
*/ | ||
public static boolean isEmpty(String value) { | ||
|
||
return (value == null) || (value.length() == 0); | ||
} | ||
|
||
/** | ||
* extract file name form the given file path | ||
* @param filePath path to the file, like 'c:/test.jpg', 'c:\\test.jpg' | ||
* @param withExtention indicate contain file.extention. true : contain | false : ignore | ||
* @return fileName file.name; | ||
*/ | ||
public static String getFileName(String filePath,boolean withExtention){ | ||
int sep = filePath.lastIndexOf("\\") == -1 ? filePath.lastIndexOf("/") : filePath.lastIndexOf("\\"); | ||
if(withExtention) | ||
return filePath.substring( sep + 1 ); | ||
return filePath.substring( sep + 1 , filePath.lastIndexOf(".")); | ||
} | ||
|
||
/** | ||
* get path to the given file , e.g. : c:\test\aaa.html -> c:\test\ | ||
* @param fileFullPath path to file; | ||
* @return | ||
*/ | ||
public static String getFilePath(String fileFullPath){ | ||
int sep = fileFullPath.lastIndexOf("\\") == -1 ? fileFullPath.lastIndexOf("/") : fileFullPath.lastIndexOf("\\"); | ||
return fileFullPath.substring(0, sep + 1); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* | ||
*/ | ||
package com.albert.servlet; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Collection; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.MultipartConfig; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.Part; | ||
|
||
import org.apache.poi.ConvertFacade; | ||
import org.apache.poi.hwpf.converter.DocConverter; | ||
|
||
import com.albert.domain.ResponseEntity; | ||
|
||
/** | ||
* @ClassName: PoiServlet | ||
* @Description: | ||
* @author albert | ||
* @date 2017年9月19日 下午12:41:36 | ||
* | ||
*/ | ||
@WebServlet(value = "/poi") | ||
@MultipartConfig | ||
public class PoiServlet extends BaseServlet { | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = 7890806721814771129L; | ||
|
||
public void convert() throws Exception { | ||
|
||
String savePath = req.getServletContext().getRealPath("/uploadFile"); | ||
File dir = new File(savePath); | ||
if(!dir.exists()){ | ||
dir.mkdirs(); | ||
} | ||
Collection<Part> parts = req.getParts(); | ||
if (parts.size() == 1) { | ||
Part part = req.getPart("file"); | ||
String header = part.getHeader("content-disposition"); | ||
// 获取文件名 | ||
String fileName = getFileName(header); | ||
// 把文件写到指定路径 | ||
part.write(savePath + File.separator + fileName); | ||
String htmlFile = savePath+File.separator +fileName.replace(".","")+".html"; | ||
ConvertFacade.convert(savePath + File.separator + fileName,htmlFile); | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(htmlFile),"utf-8")); //这里可以控制编码 | ||
StringBuffer sb = new StringBuffer(); | ||
String line = null; | ||
while((line = br.readLine()) != null) { | ||
sb.append(line); | ||
} | ||
|
||
write(resp, ResponseEntity.success(sb.toString())); | ||
} | ||
} | ||
|
||
public String getFileName(String header) { | ||
String[] tempArr1 = header.split(";"); | ||
String[] tempArr2 = tempArr1[2].split("="); | ||
String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", ""); | ||
return fileName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,5 +71,4 @@ public void printdata() throws IOException { | |
write(resp,ResponseEntity.failed(e.getMessage())); | ||
} | ||
} | ||
//...其他方法 | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* | ||
*/ | ||
package org.apache.poi; | ||
|
||
import org.apache.poi.hslf.converter.PPTConverter; | ||
import org.apache.poi.hssf.converter.XlsConverter; | ||
import org.apache.poi.hwpf.converter.DocConverter; | ||
import org.apache.poi.xssf.converter.XlsxConverter; | ||
import org.apache.poi.xwpf.converter.DocxConverter; | ||
|
||
/** | ||
* @ClassName: ConvertFacade | ||
* @Description: 转换门面类 | ||
* @author albert | ||
* @date 2017年9月18日 下午4:52:00 | ||
* | ||
*/ | ||
public class ConvertFacade { | ||
public final static String DOC = "doc"; | ||
public final static String DOCX = "docx"; | ||
public final static String XLS = "xls"; | ||
public final static String XLSX = "xlsx"; | ||
public final static String PPT = "ppt"; | ||
|
||
public static void convert( String filePath, String output ) throws Exception{ | ||
if(!filePath.contains(".")) throw new Exception("不合法的文件名"); | ||
switch (filePath.substring(filePath.lastIndexOf(".")+1).toLowerCase()) { | ||
case DOC: | ||
DocConverter.convert(filePath, output); | ||
break; | ||
case DOCX: | ||
DocxConverter.convert(filePath, output); | ||
break; | ||
case XLS: | ||
XlsConverter.convert(filePath, output); | ||
break; | ||
case XLSX: | ||
XlsxConverter.convert(filePath, output); | ||
break; | ||
case PPT: | ||
PPTConverter.convert(filePath, output); | ||
break; | ||
default: | ||
throw new Exception("不合法的文件"); | ||
} | ||
} | ||
} |
Oops, something went wrong.