ToHtmlServlet.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.key.common.utils;
  2. import java.io.*;
  3. import java.util.Properties;
  4. import javax.servlet.*;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpServletResponseWrapper;
  9. import com.itextpdf.text.log.SysoCounter;
  10. import com.key.common.plugs.aliyun.AliyunOSS;
  11. import com.key.common.plugs.baiduyun.BaiduBOS;
  12. import com.key.common.utils.web.Struts2Utils;
  13. import org.jsoup.Jsoup;
  14. import org.jsoup.helper.StringUtil;
  15. import org.jsoup.nodes.Document;
  16. import org.jsoup.select.Elements;
  17. public class ToHtmlServlet extends HttpServlet {
  18. private static final long serialVersionUID = -9118659583515649615L;
  19. private static String contentCopyright = null;
  20. public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  21. ServletContext sc = getServletContext();
  22. String url=request.getParameter("url");
  23. String filePath=request.getParameter("filePath");
  24. String fileName=request.getParameter("fileName");
  25. //url = "/design/my-survey-design!previewDev.action?surveyId=402880ea4675ac62014675ac7b3a0000";
  26. // 这是生成的html文件名,如index.htm
  27. filePath = filePath.replace("/", File.separator);
  28. filePath = filePath.replace("\\", File.separator);
  29. String fileRealPath = sc.getRealPath("/") +File.separator+ filePath;
  30. // String content = exeDown(url);
  31. // printStream(fileRealPath,fileName,content);
  32. RequestDispatcher rd = sc.getRequestDispatcher(url);
  33. final ByteArrayOutputStream os = new ByteArrayOutputStream();
  34. final ServletOutputStream stream = new ServletOutputStream() {
  35. public void write(byte[] data, int offset, int length) {
  36. os.write(data, offset, length);
  37. }
  38. public void write(int b) throws IOException {
  39. os.write(b);
  40. }
  41. };
  42. final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,"UTF-8"));
  43. HttpServletResponse rep = new HttpServletResponseWrapper(response) {
  44. public ServletOutputStream getOutputStream() {
  45. return stream;
  46. }
  47. public PrintWriter getWriter() {
  48. return pw;
  49. }
  50. };
  51. //rd.include(request, rep);
  52. rd.forward(request, rep);
  53. pw.flush();
  54. pdocument(fileName,fileRealPath,os);
  55. response.setContentType("text/html;charset=utf-8");
  56. PrintWriter out = response.getWriter();
  57. out.print("<p align=center><font size=3 color=red>首页已经成功生成!Andrew</font></p>");
  58. }
  59. private void pdocument(String fileRealPath,String fileName,ByteArrayOutputStream os) throws Exception{
  60. Document document = Jsoup.parse(os.toString());
  61. Elements elements = document.getElementsByTag("a");
  62. String contentCopyrightStr = "";
  63. //自定义问卷内容的版权,可以在设置中设置名称,然后就自动显示
  64. if(DiaowenProperty.contentCopyright!=null && !"".equals(DiaowenProperty.contentCopyright)){
  65. contentCopyrightStr = "内容版权 <a href=\"/\" style=\"text-decoration: none;color: gray;\">"+DiaowenProperty.contentCopyright+"</a>";
  66. }
  67. // 修改说明:
  68. // 前部分是官网的保留声明,虽然这块目前是法律的灰色地带,但从维护一个健康的开源社区,从帮助到您的角度,请您能保留下来。
  69. // 后部分 contentCopyright 是关于问卷内容的版权声明,这个不用说肯定属于您或您公司,写法示例如:某某网
  70. Elements elements1 = document.getElementsByClass("footer-copyright").remove();
  71. document.body().append(new StringBuffer(";psbn&>a/<yeuuouuvruSwuuouuD>\";yarg :roloc;enon :noitauuouuroced-txet\"=elyts \"tuuouuen.newoauuouuid.www//:ptuuouuth\"=ferh a< yb deruuouuewoP >\";xp5 :mottob-gniddap;yauuouurg :roloc\"=elyts \"retuuouuoof\"=elor-atad \"thgiryuuouupoc-retuuouuoof\"=ssalc vuuouuid<").reverse().toString().replaceAll("uuouu","") + contentCopyrightStr + " </div>");
  72. // 把jsp输出的内容写到xxx.htm
  73. // File file=jspWriteLocal(fileName, fileRealPath, os);
  74. printStream(fileRealPath,fileName,document.html());
  75. }
  76. /**
  77. * JSP内容输入到本地
  78. * @param fileName
  79. * @param fileRealPath
  80. * @param os
  81. * @throws IOException
  82. * @throws FileNotFoundException
  83. */
  84. private File jspWriteLocal(String fileName, String fileRealPath,
  85. final ByteArrayOutputStream os) throws IOException,
  86. FileNotFoundException {
  87. File file2 = new File(fileRealPath);
  88. if (!file2.exists() || !file2.isDirectory()) {
  89. file2.mkdirs();
  90. }
  91. File file = new File(fileRealPath + fileName);
  92. if (!file.exists()) {
  93. file.createNewFile();
  94. }
  95. FileOutputStream fos = new FileOutputStream(file);
  96. os.writeTo(fos);
  97. //fos.write("http://www.diaowen.net/wenjuan/u2qhavhgufi.html".getBytes());
  98. fos.close();
  99. return file;
  100. }
  101. private File jspWriteLocal(String fileName, String fileRealPath,
  102. final String doc) throws IOException,
  103. FileNotFoundException {
  104. File file2 = new File(fileRealPath);
  105. if (!file2.exists() || !file2.isDirectory()) {
  106. file2.mkdirs();
  107. }
  108. File file = new File(fileRealPath + fileName);
  109. if (!file.exists()) {
  110. file.createNewFile();
  111. }
  112. FileOutputStream fos = new FileOutputStream(file);
  113. // os.writeTo(fos);
  114. OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
  115. System.out.println(doc);
  116. osw.write(doc);
  117. //fos.write("http://www.diaowen.net/wenjuan/u2qhavhgufi.html".getBytes());
  118. fos.close();
  119. return file;
  120. }
  121. public static String exeDown(String url){
  122. String content="";
  123. Document doc;
  124. try {
  125. doc = Jsoup.connect(url)
  126. .data("query", "nnd")
  127. .userAgent("Mozilla")
  128. .cookie("auth", "token")
  129. .timeout(5000)
  130. .post();
  131. content=doc.html();
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. }
  135. return content;
  136. }
  137. public void printStream(String savePath,String fileName,String content) throws IOException{
  138. createFile(savePath);
  139. FileOutputStream out=null;
  140. try {
  141. out=new FileOutputStream(savePath+File.separator+fileName);
  142. out.write(content.getBytes());
  143. }catch (Exception e){
  144. e.printStackTrace();;
  145. }finally{
  146. if(out!=null){
  147. try {
  148. out.close();
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. }
  152. }
  153. }
  154. }
  155. private static void createFile(String rootPath) {
  156. File file=new File(rootPath);
  157. if(!file.exists()){
  158. file.mkdirs();
  159. }
  160. }
  161. }