package cn.ksource.core.util;
|
|
import java.io.BufferedReader;
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.io.UnsupportedEncodingException;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.Map.Entry;
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
import org.apache.poi.poifs.filesystem.DirectoryEntry;
|
import org.apache.poi.poifs.filesystem.DocumentEntry;
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
import org.jsoup.Jsoup;
|
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Element;
|
import org.jsoup.select.Elements;
|
|
import cn.ksource.config.SysConfigConstants;
|
|
public class HtmlToword {
|
|
|
public static String writeWordFile(HttpServletRequest request,HttpServletResponse response,String url,Map<String, String> addParams,String fileName) {
|
|
|
try {
|
String content = get(url, addParams);
|
|
/* Map<String,String> map = zoomImage(request,content);
|
content = map.get("result");
|
String imgSrc = map.get("imgSrc");
|
System.out.println(imgSrc);*/
|
byte b[] = content.getBytes();
|
ByteArrayInputStream bais = new ByteArrayInputStream(b);
|
POIFSFileSystem poifs = new POIFSFileSystem();
|
DirectoryEntry directory = poifs.getRoot();
|
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
poifs.writeFilesystem(baos);
|
bais.close();
|
toClientForReport(baos,fileName,response);
|
|
//删除临时图像
|
/* String path = request.getSession().getServletContext().getRealPath("/")+imgSrc;
|
return path;*/
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
public static Map<String, String> zoomImage(HttpServletRequest request,String content) {
|
Document document = Jsoup.parse(content);
|
Elements elements = document.select("img");
|
Map map = new HashMap();
|
if(elements.size()>0) {
|
Element element = elements.get(0);
|
String imgSrc = element.attr("src");
|
System.out.println(imgSrc);
|
imgSrc = imgSrc.replace(SysConfigConstants.SERVER_IP, "");
|
String pathPix = request.getSession().getServletContext().getRealPath("/");
|
System.out.println(imgSrc);
|
String newName = FileUtil.resizeForEqualImg(new File(pathPix+imgSrc), 196, 184, false);
|
System.out.println(newName);
|
imgSrc = imgSrc.replace(imgSrc.substring(imgSrc.lastIndexOf("/")+1), newName);
|
map.put("imgSrc", imgSrc);
|
System.out.println(SysConfigConstants.SERVER_IP+imgSrc);
|
element.attr("src", SysConfigConstants.SERVER_IP+imgSrc);
|
}
|
String result = document.toString();
|
map.put("result", result);
|
return map;
|
}
|
|
|
public static String downloadWordFile(HttpServletRequest request,HttpServletResponse response,String url,String path,Map<String, String> addParams) {
|
boolean w = false;
|
|
try {
|
if (!"".equals(path)) {
|
// 检查目录是否存在
|
File fileDir = new File(path);
|
if (!fileDir.exists()) {
|
fileDir.mkdirs();
|
}
|
// 生成临时文件名称
|
String fileName = StringUtil.getUUID()+".doc";
|
String content = get(url, addParams);
|
|
|
byte b[] = content.getBytes();
|
ByteArrayInputStream bais = new ByteArrayInputStream(b);
|
POIFSFileSystem poifs = new POIFSFileSystem();
|
DirectoryEntry directory = poifs.getRoot();
|
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
|
FileOutputStream ostream = new FileOutputStream(path+ fileName);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
poifs.writeFilesystem(ostream);
|
bais.close();
|
ostream.close();
|
|
return fileName;
|
}
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return "";
|
}
|
|
|
public static void downloadhtml(HttpServletResponse response,String url,Map<String, String> addParams,String fileName) {
|
try {
|
String content = get(url, addParams);
|
System.out.println(content);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
baos.write(content.getBytes("UTF-8"));
|
toClientForHtml(baos,fileName,response);
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
|
/** 返回客户端信息
|
* @throws UnsupportedEncodingException
|
* */
|
private static void toClientForReport(ByteArrayOutputStream baos,String name,HttpServletResponse response) throws UnsupportedEncodingException{
|
response.setHeader("Content-Type", "application/msword");
|
response.setHeader("Content-Disposition", "attachment;filename=\""+new String(name.getBytes("gbk"),"ISO8859-1")+".doc\"");
|
response.setCharacterEncoding("UTF-8");
|
try {
|
ServletOutputStream out = response.getOutputStream();
|
baos.writeTo(out);
|
baos.flush();
|
baos.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
/** 返回客户端信息
|
* @throws UnsupportedEncodingException
|
* */
|
private static void toClientForHtml(ByteArrayOutputStream baos,String name,HttpServletResponse response) throws UnsupportedEncodingException{
|
response.setHeader("Content-Type", "application/html");
|
response.setHeader("Content-Disposition", "attachment;filename=\""+new String(name.getBytes("gbk"),"ISO8859-1")+".html\"");
|
response.setCharacterEncoding("UTF-8");
|
try {
|
ServletOutputStream out = response.getOutputStream();
|
baos.writeTo(out);
|
baos.flush();
|
baos.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
|
|
public static void deleteUploadFile(String deleteFilePath){
|
File file=new File(deleteFilePath);
|
if(file.exists()){
|
if(file.delete()){
|
file.getParentFile().delete();
|
System.out.println("文件被成功删除");
|
}
|
}
|
}
|
|
|
|
public static String get(String url, Map<String, String> addParams) {
|
String result = "";
|
|
HttpURLConnection huc = null;
|
BufferedReader br = null;
|
try
|
{
|
if (addParams != null && addParams.size() > 0)
|
{
|
if (url.indexOf("?") == -1)
|
{
|
url = url + "?";
|
}
|
Set<Entry<String, String>> paramSet = addParams.entrySet();
|
for (Entry<String, String> param : paramSet)
|
{
|
url = url + "&" + param.getKey() + "=" + param.getValue();
|
}
|
}
|
|
URL u = new URL(url);
|
|
huc = (HttpURLConnection) u.openConnection();
|
huc.setConnectTimeout(2000);
|
|
br = new BufferedReader(new InputStreamReader(huc.getInputStream(), "UTF-8"));
|
|
StringBuilder sb = new StringBuilder();
|
String tmp = null;
|
while ((tmp = br.readLine()) != null) {
|
sb.append(tmp);
|
}
|
result = sb.toString();
|
}
|
catch (Exception e)
|
{
|
e.printStackTrace();
|
}
|
finally
|
{
|
if (br != null)
|
{
|
try {
|
br.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (huc != null)
|
{
|
huc.disconnect();
|
}
|
}
|
|
return result;
|
}
|
|
}
|