exportservice.jsp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <%@ page
  2. contentType="text/html;charset=utf-8"
  3. %><%@page import="java.util.*, java.io.*, java.lang.String, java.text.*" %><%
  4. request.setCharacterEncoding("utf-8");
  5. //------------------------------------
  6. // 0. 파라메터 받음
  7. //------------------------------------
  8. String pOOF = (request.getParameter("oof") == null ? "" : request.getParameter("oof"));
  9. String pFileName = (request.getParameter("filename") == null ? "" : request.getParameter("filename"));
  10. String pFileType = (request.getParameter("filetype") == null ? "" : request.getParameter("filetype"));
  11. //pOOF = "<?xml version='1.0' encoding='utf-8'?><oof version ='3.0'><document enable-thread='0'><file-list><file type='reb' path='http://localhost:8080/RexServer30/rebfiles/samples/xml_customers_orders.reb'></file></file-list><connection-list><connection type='file' namespace='*'><config-param-list><config-param name='path'>http://localhost:8080/RexServer30/rebfiles/samples/sqlserver_customers_orders.xml</config-param></config-param-list><content content-type='xml'><content-param name='root'>{%dataset.xml.root%}</content-param><content-param name='preservedwhitespace'>1</content-param><content-param name='bindmode'>name</content-param></content></connection></connection-list><field-list><field name='한 글 123 abc'><![CDATA[한글 파라메터]]></field></field-list></document></oof>";
  12. //------------------------------------
  13. // 1. 상수 정의
  14. //------------------------------------
  15. String sBasePath = "D:\\rexpert30\\RexServer30\\export\\";
  16. String sExporter = "D:\\rexpert30\\RexServer30\\bin\\export.dos.exe";
  17. String sessionid = session.getId();
  18. Date date = new Date();
  19. //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  20. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  21. String sDateTime = sdf.format( date);
  22. String sOOFFileName = sDateTime + "_" + pFileName + "_" + sessionid + ".xml";
  23. String sExportFileNameSource = sDateTime + "_" + pFileName + "_" + sessionid + "." + pFileType;
  24. String sExportFileNameTarget = pFileName + "." + pFileType;
  25. //System.out.println(sExportFileNameSource);
  26. //System.out.println(sExportFileNameTarget);
  27. //deleteFile(sBasePath + "" + sOOFFileName);
  28. deleteFile(sBasePath + "" + sExportFileNameSource);
  29. //------------------------------------
  30. // 2. OOF, xml data, img 데이터 파일로 저장
  31. //------------------------------------
  32. saveFile(sBasePath + sOOFFileName, pOOF);
  33. //System.out.println(sBasePath + sXmlDataFile);
  34. //saveFile(sBasePath + sXmlDataFile, pData);
  35. //------------------------------------
  36. // 3. pdf파일 export 저장
  37. //------------------------------------
  38. Runtime rt = Runtime.getRuntime();
  39. //System.out.println(pOOF);
  40. //System.out.println(sBasePath + sExportFileNameSource);
  41. try {
  42. Process proc;
  43. //proc = rt.exec(new String[]{sExporter, "load", pOOF, pFileType, sBasePath + sExportFileNameSource});
  44. proc = rt.exec(new String[]{sExporter, "path", sBasePath + sOOFFileName, pFileType, sBasePath + sExportFileNameSource});
  45. proc.waitFor();
  46. } catch(Exception e) {
  47. throw new Exception(e.getMessage());
  48. }
  49. //------------------------------------
  50. // 4. export 파일 읽어서 전송
  51. //------------------------------------
  52. File file = new File(sBasePath + "" + sExportFileNameSource); // 절대경로
  53. byte b[] = new byte[4062];
  54. response.reset();
  55. String strClient=request.getHeader("User-Agent");
  56. if(strClient.indexOf("MSIE 5.5")>-1)
  57. {
  58. response.setContentType("Content-type: application/x-msdownload; charset=euc-kr");
  59. response.setHeader("Content-Disposition", "attachment;filename="+new String(sExportFileNameTarget.getBytes("utf-8"),"8859_1"));
  60. } else {
  61. response.setContentType("Content-type: application/x-msdownload; charset=euc-kr");
  62. response.setHeader("Content-Disposition", "attachment;filename="+new String(sExportFileNameTarget.getBytes("utf-8"),"8859_1"));
  63. }
  64. response.setHeader("Content-Transfer-Encoding", "binary;");
  65. response.setHeader("Pragma", "no-cache;");
  66. response.setHeader("Expires", "0;");
  67. response.setContentLength((int)file.length()); //파일크기를 브라우저에 알려준다.
  68. // IE 5.5는 형식이 다르므로 헤더를 각각 다르게 처리해 준다.
  69. if (file.isFile())
  70. {
  71. BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
  72. BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());
  73. int read = 0;
  74. while ((read = fin.read(b)) != -1){
  75. outs.write(b,0,read);
  76. outs.flush();
  77. }
  78. outs.close();
  79. fin.close();
  80. }
  81. file = null;
  82. //------------------------------------
  83. // 5. pdf, xml, img 파일 삭제
  84. //------------------------------------
  85. deleteFile(sBasePath + "" + sOOFFileName);
  86. deleteFile(sBasePath + "" + sExportFileNameSource);
  87. %><%!
  88. //******************************************************************//
  89. // 공통 함수
  90. //******************************************************************//
  91. public void deleteFile(String sFile) {
  92. File file = null;
  93. try {
  94. file = new File(sFile);
  95. if(file.exists()) {
  96. if (!file.delete()) {
  97. System.gc();
  98. Thread.sleep(200);
  99. file.delete();
  100. }
  101. }
  102. } catch(Exception e) {
  103. //
  104. System.out.println(e);
  105. }
  106. }
  107. // 문자열 replace
  108. public String replaceStr(String content, String old_str, String new_str) {
  109. StringBuffer rtn = new StringBuffer();
  110. int from_idx = 0;
  111. int to_idx = 0;
  112. while ((to_idx = content.indexOf(old_str, from_idx)) >= 0) {
  113. rtn.append(content.substring(from_idx, to_idx));
  114. rtn.append(new_str);
  115. from_idx = to_idx + old_str.length();
  116. }
  117. if (from_idx == 0) {
  118. rtn.append(content);
  119. } else if (from_idx < content.length()) {
  120. rtn.append(content.substring(from_idx));
  121. }
  122. return rtn.toString();
  123. } // replaceStr
  124. // 문자열 split
  125. public String[] fnSplit(String sDelimiter, String sValue) {
  126. int index = sValue.indexOf(sDelimiter);
  127. List list = new ArrayList();
  128. int currPos = 0;
  129. int len = sDelimiter.length();
  130. while (index != -1)
  131. {
  132. list.add(sValue.substring(currPos, index));
  133. currPos = index + len;
  134. index = sValue.indexOf(sDelimiter, currPos);
  135. } // end while
  136. list.add(sValue.substring(currPos));
  137. String[] vAddString = (String[]) list.toArray(new String[list.size()]);
  138. return vAddString;
  139. } // fnSplit
  140. // 파일 저장
  141. boolean saveFile(String fileName, String inputString) {
  142. File f = new File(fileName);
  143. ByteArrayInputStream is = null;
  144. FileOutputStream fos= null;
  145. BufferedOutputStream bos = null;
  146. try{
  147. f.createNewFile();
  148. is = new ByteArrayInputStream(inputString.getBytes("UTF-8"));
  149. fos = new FileOutputStream(f);
  150. bos = new BufferedOutputStream(fos);
  151. // 파일에 저장
  152. int su=0;
  153. byte b[] = new byte[512];
  154. while((su=is.read(b)) != -1){
  155. fos.write(b,0,su);
  156. }
  157. }catch(IOException ie){
  158. ie.printStackTrace();
  159. return false; // 예외가 발생하면 실패
  160. }finally{
  161. try {
  162. bos.close();
  163. fos.close();
  164. } catch (IOException e) {}
  165. }
  166. return true;
  167. }
  168. %>