本文章作用于将多个文档写入同一个word,将其下载
public void importWord(HttpServletRequest request,HttpServletResponse response) throws IOException {
int pId = Integer.parseInt(request.getParameter("pid"));
String module=request.getParameter("module");
//查询出文档model,进行下载 importWord方法在下面
String path= wikiService.importWord(pId,module);
File f = new File(path);
FileInputStream in = null;
OutputStream out = null;
if (f.exists()) {
try {
// 1.读取要下载的内容
in = new FileInputStream(f);
String filename = f.getName();
// 2. 告诉浏览器下载的方式以及一些设置
// 解决文件名乱码问题,获取浏览器类型,转换对应文件名编码格式,IE要求文件名必须是utf-8, firefox要求是iso-8859-1编码
String agent = request.getHeader("user-agent");
if (agent.contains("FireFox")) {
filename = new String(filename.getBytes("UTF-8"), "iso-8859-1");
} else {
filename = URLEncoder.encode(filename, "UTF-8");
}
// 设置下载文件的mineType,告诉浏览器下载文件类型
String mineType = request.getServletContext().getMimeType(filename);
response.setContentType(mineType);
// 设置一个响应头,无论是否被浏览器解析,都下载
response.setHeader("Content-disposition", "attachment; filename=" + filename);
// 将要下载的文件内容通过输出流写到浏览器
out = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
}
}
}
List<PageModel> list = wordRepository.findAllByModuleAndPid(module, pid);
Document document = new Document(PageSize.A4);
String path = exportDoc(list, determineTheCeategory(module, pid), document);
return path;
public String exportDoc(List<PageModel> list1, String name, Document document) {
String wordName = name + ".doc";
String path = "C:\\Words";
//转化成vo类集合
List<PageVo> voList = TreeUtils.modelList2VoList(list1);
try {
try {
File file1 = new File(path);
if (!file1.exists()) {
file1.mkdirs();
}
File file = new File(path + "\\" + "" + wordName);
RtfWriter2.getInstance(document, new FileOutputStream(file));
document.open();
for (WikiPageVo string : voList) {
// code
if (string.getParentId() == 0) {
String content = " " + string.getContent();
Paragraph title = new Paragraph(string.getName());
//标题1
RtfParagraphStyle rtfGsBt1 = RtfParagraphStyle.STYLE_HEADING_1;
rtfGsBt1.setAlignment(Element.ALIGN_CENTER);
rtfGsBt1.setStyle(Font.BOLD);
rtfGsBt1.setSize(24);
title.setFont(rtfGsBt1);
document.add(title);
// 操作
//根据换行标签截取
String[] split1 = content.split("\n");
for (int i = 0; i < split1.length; i++) {
String s = split1[i];
if (s.contains(".png")) {
if (isContainChinese(s)) {
//不正规的URL
continue;
}
//项目图片存放在该服务,所以带着这个url
if (s.contains("http://192.168.20.179:8888")) {
//图片 获取URL
String imgUrl = org.apache.commons.lang3.StringUtils.substringBefore(org.apache.commons.lang3.StringUtils.substringAfter(s, "("), ")");
com.lowagie.text.Image img = Image.getInstance(imgUrl);
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.ALIGN_CENTER);// 设置图片显示位置
img.scalePercent(40);// 表示显示的大小为原尺寸的50%
document.add(img);
}
} else {
//字体加粗
if (s.equals("::: hljs-center") || s.equals(":::")) {
continue;
}
if (s.endsWith("**") || s.startsWith("#")) {
s = org.apache.commons.lang3.StringUtils.substringBefore(org.apache.commons.lang3.StringUtils.substringAfter(s, "**"), "**");
Paragraph codeContentStyle = new Paragraph(s, FontFactory
.getFont("STSongStd-Light", 11, Font.BOLD));
if (split1[i - 1].equals("::: hljs-center") || split1[i - 2].equals("::: hljs-center")) {
codeContentStyle.setAlignment(Element.ALIGN_CENTER);
}
document.add(codeContentStyle);
} else {
Paragraph codeContentStyle = new Paragraph(s, FontFactory
.getFont("STSongStd-Light", 11, Font.NORMAL));
document.add(codeContentStyle);
}
}
}
//子文档
test03(string, voList, document);
}
}
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BadElementException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return path + "\\" + "" + wordName;
}
public WikiPageVo test03(WikiPageVo vo1, List<WikiPageVo> voList, Document document) {
/* 设置标题3格式 */
RtfParagraphStyle rtfGsBt2 = RtfParagraphStyle.STYLE_HEADING_3;
rtfGsBt2.setAlignment(Element.ALIGN_LEFT);
rtfGsBt2.setStyle(Font.BOLD);
rtfGsBt2.setSize(18);
try {
for (WikiPageVo vo2 : voList) {
if (vo1.getId().equals(vo2.getParentId())) {
String name = vo2.getName();
String content = " " + vo2.getContent();
Paragraph title = new Paragraph(name);
title.setFont(rtfGsBt2);
document.add(title);
// 操作
String[] split1 = content.split("\n");
for (int i = 0; i < split1.length; i++) {
String s = split1[i];
if (s.contains(".png")) {
if (s.contains("http://192.168.20.179:8888")) {
//图片 获取URL
String imgUrl = org.apache.commons.lang3.StringUtils.substringBefore(org.apache.commons.lang3.StringUtils.substringAfter(s, "("), ")");
if (isContainChinese(imgUrl)) {
//不正规的URL
continue;
}
com.lowagie.text.Image img = Image.getInstance(imgUrl);
img.setAbsolutePosition(0, 0);
img.setAlignment(Image.ALIGN_CENTER);// 设置图片显示位置
img.scalePercent(40);// 表示显示的大小为原尺寸的50%
document.add(img);
}
} else {
if (s.equals("::: hljs-center") || s.equals(":::")) {
continue;
}
if (s.endsWith("**") || s.startsWith("#")) {
s = s.replaceAll("\\**", "");
s.replace("#", "");
Paragraph codeContentStyle = new Paragraph(s, FontFactory
.getFont("STSongStd-Light", 11, Font.BOLD));
if (split1[i - 1].equals("::: hljs-center") || split1[i - 2].equals("::: hljs-center")) {
codeContentStyle.setAlignment(Element.ALIGN_CENTER);
}
document.add(codeContentStyle);
} else {
Paragraph codeContentStyle = new Paragraph(s, FontFactory
.getFont("STSongStd-Light", 11, Font.NORMAL));
document.add(codeContentStyle);
}
}
}
count++;
if (vo1.getChildren() == null) {
vo1.setChildren(new ArrayList<>());
}
vo1.getChildren().add(test03(vo2, voList, document));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return vo1;
}
/**
* 判断字符串中是否包含中文
*
* @param str 待校验字符串
* @return 是否为中文
* @warn 不能校验是否为中文标点符号
*/
public boolean isContainChinese(String str) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}